Get the List of Elements in Selenium Script

Author - Manpreet Singh

How to Get the List of Elements in Selenium Script?

List is the collection of similar type of data. You can add, remove, iterate over stuff and can also query how much data is in the List.

Java.util.List is a child interface of Collection.

To get the elements of the list, we are using the following syntax:

 List<WebElement> elements = Driver.findElements(By);

we are using the findElements method because it returns the list of elements on the page. By using the above syntax we can fetch the list of elements (whose criteria is mentioned in By parameter). To traverse all the elements of the list we can use foreach loop and we can perform any operation on the elements such as get the text using the getText()); method, get the attribute value using the getAttribute(“value”); method and get ccs value using the getCssValue(“value”) method or anything user wants to perform.

For Example:
Elements in Selenium Script

In the above example we are printing the size of the List i.e how much elements exist in the List
Elements in Selenium Script

By using the foreach loop we can traverse to each element of the list and using the getText(); method we get the text associated with the each element and print it in the console using println(); method.

Result:
Elements in Selenium Script

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *