You are reading the article Findelements In Selenium – Findelement By Xpath updated in October 2023 on the website Dacquyenphaidep.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Findelements In Selenium – Findelement By Xpath
Why do you need Find Element(s) command?Interaction with a web page requires a user to locate the web element. Find Element command is used to uniquely identify a (one) web element within the web page. Whereas, Find Elements command is used to uniquely identify the list of web elements within the web page. There are multiple ways to uniquely identify a web element within the web page such as ID, Name, Class Name, Link Text, Partial Link Text, Tag Name and XPATH.
FindElement command syntax:WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));
Selenium Find Element command takes in the By object as the parameter and returns an object of type list WebElement in Selenium. By object in turn can be used with various locator strategies such as find element by ID Selenium, Name, Class Name, XPATH etc. Below is the syntax of FindElement command in Selenium web driver.
Locator Strategy can be any of the following values.
ID
Selenium find element by Name
Class Name
Tag Name
Link Text
Partial Link Text
XPATH
Locator Value is the unique value using which a web element can be identified. It is the responsibility of developers and testers to make sure that web elements are uniquely identifiable using certain properties such as ID or name.
Example:
WebElement loginLink = driver.findElement(By.linkText("Login"));
Example: Find Element in SeleniumThe following application is used for demo purpose
Scenario:Step 1: Open the AUT
package com.sample.stepdefinitions; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class NameDemo { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\3rdparty\chrome\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); } } FindElements command syntax:FindElements in Selenium command takes in By object as the parameter and returns a list of web elements. It returns an empty list if there are no elements found using the given locator strategy and locator value. Below is the syntax of find elements command.
Example:
Example: Find Elements in Selenium Scenario:Step 1: Open the URL for Application Under Test
Step 1: Find the text of radio buttons and print it onto the output console
package com.sample.stepdefinitions; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class NameDemo { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe"); WebDriver driver = new ChromeDriver(); System.out.println("Number of elements:" +elements.size()); for (int i=0; i<elements.size();i++){ System.out.println("Radio button text:" + elements.get(i).getAttribute("value")); } } } Find element Vs Find elementsBelow are the major differences between find element and find elements commands.
Find element Vs Find elements in Selenium
Find Element Find Elements
Returns the first most web element if there are multiple web elements found with the same locator Returns a list of web elements
Throws exception NoSuchElementException if there are no elements matching the locator strategy Returns an empty list if there are no web elements matching the locator strategy
Find element by XPath will only find one web element It will find a collection of elements whose match the locator strategy.
Not Applicable Each Web element is indexed with a number starting from 0 just like an array
Summary:
Find Element command returns the web element that matches the first most element within the web page.
Find Elements command returns a list of web elements that match the criteria.
Find Element by XPath in Selenium command throws NoSuchElementException if it does not find the element matching the criteria.
Find Elements command returns an empty list in Selenium if there are no elements matching the criteria
You're reading Findelements In Selenium – Findelement By Xpath
Update the detailed information about Findelements In Selenium – Findelement By Xpath on the Dacquyenphaidep.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!