Make Selenium Easy

findElement() And findElements() Methods in Selenium WebDriver

Last updated on July 8th, 2020 at 12:27 am

A website is a collection of web pages and every webpage may contain multiple elements like buttons, text boxes, calendars, checkboxes, texts, links, headers etc. These elements are categorized as web elements in general. These elements are used to create the structure of a web page and to present some specific information.

Let’s see a web page of a website DemoQA and identify web elements (Highlighted in the red box) of it:-

Selenium is a set of tool which is used to automate a browser or a web application. In this post, we will see a specific tool of Selenium called Selenium WebDriver.

When we access any website manually then we can visualize web elements and perform actions like typing into a text box, click on a link, select a date in a calendar, etc.

When we want to automate these operations using Selenium WebDriver then it can not visualize web elements automatically. We need to explicitly let WebDriver know where they can find a particular element to perform desired operations on them. We have already learned different ways of locating web elements on a web page.

Selenium WebDriver provides two methods to locate a web element or a list of web elements. These methods are:-

  1. findElement(By by) – To find a web element
  2. findElements(By by) – To find a list of web elements

The above methods are declared in SearchContext which are inherited in WebDriver interface. You can see in WebDriver interface, these inherited methods are annotated as @Override.

The above methods are implemented in RemoteWebDriver class. By is an abstract class. This class has a mechanism to locate elements within a document. We can create our own locating mechanism by creating a subclass of class “By“.

There are no different mechanism for findElement() and findElements() methods. Method findElement() calls findElements() method internally and return first element from list.

Class “By” contains static methods for predefined locators e.g. id(String id), linkText(String linkText) etc. The return type of all these static methods is “By”. So we can use the class “By” in findElement and findElements() methods as below:-

Since findElement() returns a single element so the return type of this method is WebElement while findElements() returns a List of WebElements so the return type of this method is a List<WebElement>.

WebElement is an interface that represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this interface. All method calls will do a freshness check to ensure that the element reference is still valid. This essentially determines whether or not the element is still attached to the DOM. If this test fails, then anStaleElementReferenceException is thrown, and all future calls to this instance will fail.

WebElement findElement(By by)

findElement(By by) method finds and returns the first matching element within the current context by the given mechanism. For example if we pass id in above method then it will find and return the first element whose id is a match. If no web element is found with the provided mechanism then it throws NoSuchElementException.

This method is affected by implicit wait which is set for a WebDriver session. A web element may not be available on the page immediately. If we want WebDriver to wait for sometimes and keep finding a web element we can set implicit wait. Since WebDriver uses findElement() method to locate a web element and if we set implicit wait then this method will be affected and NoSuchElementException will be thrown only if no web element is found with a given mechanism within the implicit timeout.

Program to locate element

Program to locate non-present element

It will immediately throw NoSuchElementException as the locator is incorrect and no implicit wait is set.

Output

Program to locate the non-present element with implicit wait

It will not immediately throw NoSuchElementException as the locator is the incorrect and implicit wait is set.

Output

Complete Program

java.util.List<WebElement> findElements(By by)

findElements(By by) finds and returns all matching elements within the current context by the given mechanism. For example, if we pass id in the above method then it will find and return all elements whose id is a match. If no web element is found with the provided mechanism then it never throws NoSuchElementException instead it returns a List with size zero.

This method is affected by implicit wait which is set for a WebDriver session. A web element may not be available on the page immediately. If we want WebDriver to wait for sometimes and keep finding a web element we can set implicit wait. Since WebDriver uses findElements() method to locate multiple web elements and if we set implicit wait then this method will be affected and an empty list will be thrown only if no web element is found with a given mechanism within the implicit timeout.

Program to locate elements

Program to locate non-present elements

Output

Program to locate the non-present elements with implicit wait

findElements() will wait till implicit timeout if the element is not available immediately. Remember it returns as soon as more than zero elements are found and does not wait till timeout.

Output

Complete Program

Interview question

  1. Why return type is List<WebElement>, not Set<WebElement>?

Reasons are as below:

  1. List allows duplicates elements and if the passed locator is matched with duplicate elements also then it returns all.
  2. List preserves the insertion order of elements which helps in the mechanism of findElement() method which internally calls findElements() and returns the first element in list which is the first found element.

2. What are differences between findElement() and findElements() method?

Refer detailed post here.

3. What are common between findElement() and findElements() methods?

  1. Both methods are affected by the ‘implicit wait’.
  2. Both methods use the same logic to find web elements, just they return it in different ways.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

4 thoughts on “findElement() And findElements() Methods in Selenium WebDriver

  1. there is a typo mistake
    ” not found while findElement() will not throw any exception. It will return an empty List.”
    Here findelements() will not throw any exception

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

findElement() And findElements() Methods in Selenium WebDriver

by Amod Mahajan time to read: 8 min
4
%d bloggers like this: