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.























