Make Selenium Easy

Selenium Interview Question 10 – Difference Between findElement() and findElements() methods

As a part of Selenium Interview Questions series, in this post, we will learn differences between findElement() and findElements() methods in Selenium WebDriver.

WebDriver interface provides two methods to locate web element/s. Those are:-

  1. findElement(By by)
  2. findElements(By by)

Difference between findElement() and findElements() are below:-

  1. findElement() is used to find first web element on current page by provided locator mechanism such as id, xpath, css etc whereas findElements() is used to find all web elements on current page by provided locator mechanism such as id, xpath, css etc.
  2. findElement() will return only first web element even provided locator locates more than one web element. findElements() returns all matching web elements. findElement() method internally calls findElements() method itself and return first indexed web element.
  3. Return type of findElement() method is a WebElement whereas return type of findElements() method is a List<WebElement>.
  4. findElement() will throw NoSuchElementException if no matching element is found. findElements() will return an empty list if no matching element is found. This is the reason findElements() is a better way for checking non-presence of web element. Kindly concentrate on this point more. NoSuchElementException is thrown by findElement() not findElements().
  5. findElement() Syntax:-
    WebElement element = driver.findElement(By.id(“someId”);
    findElement() Syntax:-
    List<WebElement> allElements =driver.findElements(By.xpath(“someXpath”);
    Where “driver” is a WebDriver reference.

There is another important points which I would like to cover here. It is not difference but common thing between methods. Its impact of Implicit Wait.

Both method findElement() and findElements() are affected by ‘Implicit wait’ times in force at the time of execution. If you do not set any impliict wait, findElement() and findElements() will poll only once and return the result. It means if element is found in first poll, findElement() will return the element otherwise NoSuchElementException . In case of findElements(), it will return a list of matching web elements or an empty list after first poll. Both method will not poll further.

But if you have set Implicit wait, both method will poll repeatedly. findElement() will poll for web element repeatedly till it finds web element or the timeout is reached. If it finds web element before timeout, it returns web element. If it could not find within timeout, it will throw NoSuchElementException exception.

findElements() will poll for web elements and return as soon as there are more than 0 items in the found collection before timeout. If it reaches timeout and finds no element, it will return an empty list.

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Table of Contents

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

2 thoughts on “Selenium Interview Question 10 – Difference Between findElement() and findElements() methods

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.

Selenium Interview Question 10 – Difference Between findElement() and findElements() methods

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