findElement and findElements in Selenium: Use Cases with Examples

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

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.

  1. 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<WebElement> 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

Find Element

Find Elements

Exception Handling

Throws a NoSuchElementException when no matching element is found.

Returns an empty list (not null) when no matching elements are found.

Purpose

Find the first matching element based on the locator you specify.

Finds all matching elements based on the locator.

Return Type

A single WebElement object.

A List of WebElements. If no elements match, it returns an empty list.

Use Case

When interacting with a single element

When working with multiple elements or checking if elements exist

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

How to use Selenium findElement

Let us see firstly how to use Selenium findElement method:

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:

findElements method in Selenium using java

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<WebElement> 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(<element ID>))

2. By Xpath:

driver.findElement(By.xpath(<xpath>))

3. By Name:

driver.findElement(By.name(<element-name>))

4. By Class Name:

driver.findElement(By.className(“<element-class>”))

5. By CSS Selector:

driver.findElement(By.cssSelector(<css-selector>))

6. By LinkText:

driver.findElement(By.linkText(<link text>))

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/blog/findelement-vs-findelement...