Need for Find Element and FindElements command?
A user is required to interact with a web page to locate the web element. The command Find Element is used to uniquely locate a web element within the web page. Whereas, the command FindElements is used to locate the list of web elements within the web page uniquely. There are many ways to identify a web element within the web page like Name, ID, Link Text, Partial Link Text, ClassName, XPath, and Tag Name.
Selenium FindElement Command:
FindElement command uses By object as a parameter and returns an object of type WebElement. By object can be used with different locator strategies like Name, ID, ClassName, XPath, LinkText, etc.
Syntax of FindElement Command
WebElement elementName = driver.findElement(By.LocatorStrategy(“LocatorValue”));
Following are the Locator Strategy values:
- Name
- ID
- Tag Name
- Class Name
- Link Text
- XPath
- Partial Link Text
Locator Value is the value through which a web element can be identified uniquely. It is the developer’s and tester’s job to make sure that web elements are identified uniquely with properties like Name or ID.
[box type=”success” align=”” class=”” width=””]
Example:
WebElement login = driver.findElement(By.linkText(‘Login”)); [/box]
Selenium FindElements Command:
FindElements command uses By object as the parameter and returns a list of web elements. It returns an empty list if no elements are found when given locator strategy and locator value.
Syntax of FindElements Command
List<WebElement> elementName = driver.findElements(By.LocatorStragety(“LocatorValue”));
[box type=”success” align=”” class=”” width=””]
Example:
List<WebElement> Elementslist = driver.findElements(By.xpath(“//div”));
[/box]
Difference between findElement and find Elements Methods
findElement | findElements |
Return single element | Returns a list of elements |
It returns only first element when multiple elements are found using the same locator | Return all elements from a list of web elements |
If no matching element found, throw NoSuchElementFound Exception | If no matching element found, then it will return an empty list |
Return single element | To get the element from the list, we need to iterate over it |
Apply to find element method over element found | Not possible to apply |
Not Applicable | Each Web Element is indexed with 0 number just like an array |
How to use Selenium findElement Command
The below application is used for demo purpose:
https://www.facebook.com/
Scenario:
- Open the https://www.facebook.com/ for AUT
- Find and click the radio button
package com.testing.selenium.findelement; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class FindElement { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.facebook.com/"); //Find the radio button for Gender "Male" by using its ID and click on it driver.findElement(By.id("u_0_7")).click(); } }
How to use Selenium find Elements Command
The below application is used for demo purpose:
https://www.facebook.com/
Scenario:
- Open the https://www.facebook.com/ for AUT
- Find the text of radio buttons and print on console
package com.testing.selenium.findelements; 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 FindElements { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.facebook.com/"); //Find the radio button for Gender "Male" by using its ID List<WebElement> elements = driver.findElements(By.id("u_0_7")); 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")); } } }
Conclusion
- The Find Element command returns a single element that matches the first element within the web page.
- The Find Elements command returns a list of elements.
- The Find Element command throws NoSuchElementException if no matching element is found.
- The Find Elements command returns an empty list if no matching element is found, it will return an empty list.
4 Responses