findElement() And findElements() Methods in Selenium WebDriver

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