All about findElement() and findElements() methods

Hello Folks,

In this post we will learn:

  1. What is findElement() method.
  2. What is findElements() method.
  3. What is difference between findElement() and findElements() methods.
  4. What is common between findElement() and findElements() methods.
  5. Exception thrown in case of element is not found.

Introduction:

A webpage has many elements like button, text box etc. These elements are called as web elements.

 

Whatever you can see in above image, all are web elements. E.g. Logo, Home, Registration label, First Name label, Text box etc.

As we know that, you need to let Selenium Webdriver  where is web element present before performing any actions on web element. To locate web element we know all the locators. Using locators we need to find web element on web page. Finding web element on web page can be done by two methods:

  1. findElements()
  2. findElement()

We have used above methods in earlier posts, but not in details. Now we will see these methods in details.

findElements():

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

findElements() takes argument of type of By class and returns all the matching web elements present in currently loaded web page only. This method returns all matching web elements in a List of type WebElement which is an interface in Collection framework.

Note: It search for the web element only on currently loaded web page, not any other or all web page.

Why return type is List<WebElement>??

Reasons are as below:

  1. List allows duplicates objects. If passed locator is matching with multiple elements, it stores all.
  2. It preserves the insertion order of objects which helps in mechanism of findElement() method.

JAVA CODE for findElements():

Output:

Total input elements found:15

Now do some mistake so that it should not be able to find web element:

I changed code as below:

List<WebElement> allInputElements= driver.findElements(By.xpath(“//iput”));

Now run the program.

Output:

Total input elements found:0

So, it does not throw any exception. It just returned an empty list immediately as soon as page is loaded completely.

findElement():

Syntax: WebElement findElement(By by)

findElement() method takes argument of By class and returns first matching web element on currently loaded web page. If passed locator is matching with multiple web elements, it will return only first matching web element.

In fact, findElement() method calls findElements() method internally, and returns first element from list.

Actual implementtaion of findElement() is as below:

JAVA CODE for findElement() method:

Let’s do some mistake in locators so that it should be unable to find web element. I did below change in above code:

WebElement registrationLabel= driver.findElement(By.xpath(“//h1[@class=’entry-t’]”));

You will get exception in console as below:

Three things to be learnt from above console output:

  1. type of Exception thrown.
  2. Till what time webdriver searched for the element. It just tried once and returned exception.
  3. At which line, exception is thrown. It is very important to fix issue in code.

Difference between findElement() and findElements() method:

  1. Return type of findElement() is a a web element while return type of findElements() is a List<WebElement>.
  2. findElement() method will throw noSuchElementException if web element is not found while findElement() will not throw any exception. It will return an empty List<WebElement>.

Common between findElement() and findElements() methods:

  1. Both methods are affected by the ‘implicit wait’ times in force at the time of execution. We will see this concept later.
  2. Both methods use the same logic to find web elements , just they return it in different ways.

That’s it. I explained all basic concepts about findElement() and findElements() method. Hope it will help you.

If you like my posts, please like, comment and share. Feedback and suggestion are always welcomed.

Thank You!!

#HappyLearning

 

 

 

 

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw 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