Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

findElement() And findElements() Methods in Selenium WebDriver

Posted on 04/23/2025 By admin

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:-

WebDriver driver = new ChromeDriver();
WebElement ele = driver.findElement(By.name("ReceiveNo"))
WebElement ele = driver.findElement(By.id("someID"))
List multipleEle = driver.findElements(By.id("someID"));

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

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.

@Test
        public void findElementExample() {

                // Browser initialization
                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                driver.get("https://www.demoqa.com/text-box");

                // Locating a element with id
                WebElement fullName = driver.findElement(By.id("userName"));
                // Perform typing
                fullName.sendKeys("Amod");

                driver.quit();
        }

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

@Test
        public void findNonPresentElementExample() {

                // Browser initialization
                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                driver.get("https://www.demoqa.com/text-box");

                System.out.println("Start Time: " + new Date());
                WebElement fullName = null;
                
                try {
                        // Locating a element with id
                        fullName = driver.findElement(By.id("userNameWrong"));
                        // Perform typing
                        fullName.sendKeys("Amod");
                } catch (NoSuchElementException e) {
                        System.out.println("End Time: " + new Date());
                        e.printStackTrace();
                }
                driver.quit();
        }
Start Time: Tue Jul 07 23:44:53 IST 2020
End Time: Tue Jul 07 23:44:53 IST 2020
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#userNameWrong"}
  (Session info: chrome=83.0.4103.116)

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

@Test
        public void findNonPresentElementExampleWihImplicitWait() {

                // Browser initialization
                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                driver.get("https://www.demoqa.com/text-box");
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

                System.out.println("Start Time: " + new Date());
                WebElement fullName = null;
                
                try {
                        // Locating a element with id
                        fullName = driver.findElement(By.id("userNameWrong"));
                        // Perform typing
                        fullName.sendKeys("Amod");
                } catch (NoSuchElementException e) {
                        System.out.println("End Time: " + new Date());
                        e.printStackTrace();
                }
                driver.quit();
        }
Start Time: Tue Jul 07 23:50:02 IST 2020
End Time: Tue Jul 07 23:50:12 IST 2020
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#userNameWrong"}
package ScenarioBased;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class FindElementExample {

        @Test
        public void findElementExample() {

                // Browser initialization
                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                driver.get("https://www.demoqa.com/text-box");

                // Locating a element with id
                WebElement fullName = driver.findElement(By.id("userName"));
                // Perform typing
                fullName.sendKeys("Amod");

                driver.quit();
        }

        @Test
        public void findNonPresentElementExample() {

                // Browser initialization
                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                driver.get("https://www.demoqa.com/text-box");

                System.out.println("Start Time: " + new Date());
                WebElement fullName = null;
                
                try {
                        // Locating a element with id
                        fullName = driver.findElement(By.id("userNameWrong"));
                        // Perform typing
                        fullName.sendKeys("Amod");
                } catch (NoSuchElementException e) {
                        System.out.println("End Time: " + new Date());
                        e.printStackTrace();
                }
                driver.quit();
        }
        
        @Test
        public void findNonPresentElementExampleWihImplicitWait() {

                // Browser initialization
                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                driver.get("https://www.demoqa.com/text-box");
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

                System.out.println("Start Time: " + new Date());
                WebElement fullName = null;
                
                try {
                        // Locating a element with id
                        fullName = driver.findElement(By.id("userNameWrong"));
                        // Perform typing
                        fullName.sendKeys("Amod");
                } catch (NoSuchElementException e) {
                        System.out.println("End Time: " + new Date());
                        e.printStackTrace();
                }
                driver.quit();
        }

}

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.

@Test public void findElementsExample() { // Browser initialization WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.demoqa.com/text-box"); // Locating a element with id List fullName = driver.findElements(By.id("userName")); System.out.println("Total elements found : "+ fullName.size()); // Perform typing fullName.get(0).sendKeys("Amod"); driver.quit(); }
@Test public void findNonPresentElementExample() { // Browser initialization WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.demoqa.com/text-box"); System.out.println("Start Time: " + new Date()); List fullName = driver.findElements(By.id("userNameWrong")); System.out.println("Total elements found : "+ fullName.size()); System.out.println("End Time: " + new Date()); driver.quit(); }
Start Time: Wed Jul 08 00:08:08 IST 2020
Total elements found : 0
End Time: Wed Jul 08 00:08:08 IST 2020

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.

@Test public void findNonPresentElementExampleWihImplicitWait() { // Browser initialization WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.demoqa.com/text-box"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); System.out.println("Start Time: " + new Date()); List fullName = driver.findElements(By.id("userNameWrong")); System.out.println("Total elements found : "+ fullName.size()); System.out.println("End Time: " + new Date()); driver.quit(); }
Start Time: Wed Jul 08 00:10:26 IST 2020
Total elements found : 0
End Time: Wed Jul 08 00:10:36 IST 2020
package ScenarioBased; import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class FindElementsExample { @Test public void findElementsExample() { // Browser initialization WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.demoqa.com/text-box"); // Locating a element with id List fullName = driver.findElements(By.id("userName")); System.out.println("Total elements found : "+ fullName.size()); // Perform typing fullName.get(0).sendKeys("Amod"); driver.quit(); } @Test public void findNonPresentElementExample() { // Browser initialization WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.demoqa.com/text-box"); System.out.println("Start Time: " + new Date()); List fullName = driver.findElements(By.id("userNameWrong")); System.out.println("Total elements found : "+ fullName.size()); System.out.println("End Time: " + new Date()); driver.quit(); } @Test public void findNonPresentElementExampleWihImplicitWait() { // Browser initialization WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.demoqa.com/text-box"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); System.out.println("Start Time: " + new Date()); List fullName = driver.findElements(By.id("userNameWrong")); System.out.println("Total elements found : "+ fullName.size()); System.out.println("End Time: " + new Date()); driver.quit(); } }
  1. Why return type is List, not Set?

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.

Refer detailed post here.

  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

Uncategorized

Post navigation

Previous Post: Git Tutorial 19 – Git Switch – Create and Switch Branches – An Alternative of Git Checkout
Next Post: Javafaker API – numerify(), letterify() and bothify() methods of Faker Class

Related Posts

image – Make Selenium Easy Uncategorized
#1. |Rest Assured Framework|Points To Consider Before Developing Test Automation Framework| Uncategorized
Make Selenium Easy – Page 4 Uncategorized
How To Verify Functionality Of Back To Top Button In Selenium WebDriver Uncategorized
Arguments – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark