DEV Community

Cover image for findElement and findElements in Selenium: Use Cases with Examples
satyaprakash behera
satyaprakash behera

Posted on

findElement and findElements in Selenium: Use Cases with Examples

When we want to interact with any web page, we use locators to identify the web elements and interact with them using locators. We use HTML elements on the web page while automating any web based application using Selenium WebDriver.

Selenium WebDriver has two methods for identifying the web elements, they are findElement and findElements.

1. findElement: This is used to uniquely identify any web element within the web page.
2. findElements: This is used to uniquely identify the list of web elements within the web page.
These methods are very important for locating web elements on a web page, but they serve different purposes and have distinct use cases.

Let us see what exactly the both methods are:

1. Find Element():

  • findElement() is a method in the WebDriver Interface in Selenium.
  • It is used to locate the first element that matches the specified locator strategy and conditions.
  • If no matching element is found, it throws a NoSuch Element Exception.
  • The result of findElement() is always a single WebElement object. The findElement() method returns an object of the type WebElement. It uses various locators such as ID, Xpath, CSS, Name and ClassName.

Below is the syntax of findElement command:

WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));
Locator Strategy comprises any one of the locators. Locator value is the unique value that identifies the web element.

2. FindElements():

  • findElements() is a method provided by the WebDriver interface in Selenium.
  • It is used to locate all the web elements that match the specified locator strategy and conditions.
  • If there are no matching elements, it returns an empty list.
  • The return type of findElements() is a list of WebElement objects. The findElements() return an empty list if no matching elements are found on the web page.

Syntax in Java:

Below is the syntax of findElements command

List elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
Locator Strategy comprises any one of the locators. Locator value is the unique value that identifies the web element.

Now let us see the differences between both methods:

Difference between find Element and find Elements in Selenium

Image description

Now let us see use cases for each of the following methods:

How to use Selenium findElement

Image description
Let us see firstly how to use Selenium findElement method:

In the above screenshot, we are finding the unique locator for the Google Search text box.

As you can see , it has the unique ID , using the ID , we have created Xpath as below:

//textarea[@id=’APjFqb’]

Below is the code snippet in java:

driver.get("https://www.google.com/")
WebElement element = driver.findElement(By.xpath("//textarea[@id='APjFqb']"));

NoSuchElementException if no matching Element is found on the page.

Now let us see the use case of the findElements method in Selenium Java:

Image description

In the above screenshot, for the registration form of IRCTC application, we have a locator class with value “col-xs-12 inputBoxPad” which matches with 8 web elements on the web page.

Below is the code in Java Selenium:

driver.get("https://www.irctc.co.in/nget/profile/user-signup");
List elements = driver.findElements(By.className("col-xs-12 inputBoxPad"));
System.out.println("Number of elements:" +elements.size());
for(int i=0; i<elements.size(); i++){
System.out.println("Input field:" + elements.get(i).getAttribute("value"));

Multiple Locator Methods Using findElement Method:

Selenium WebDriver provides the findElement(By) method to locate and interact with web elements in a script. The find Element method uses the locator object “By”. There are many ways of using “By” with locators based on the requirement.

  1. By ID:

driver.findElement(By.id())

  1. By Xpath:

driver.findElement(By.xpath())

  1. By Name:

driver.findElement(By.name())

  1. By Class Name:

driver.findElement(By.className(“”))

  1. By CSS Selector:

driver.findElement(By.cssSelector())

  1. By LinkText:

driver.findElement(By.linkText())

Conclusion

understanding the differences between findElement() and findElements() in SeleniumWebdriver Java is essential. These two methods have different purposes and have unique use cases while automating web based applications.

In brief, findElement() is used to locate and interact with a single element, throwing an exception info Element is matching. findElements() is used to locate multiple elements, returning an empty list if none are matching.

Source: This article was originally published at testgrid.io.

Top comments (0)