Get the List of Elements in Selenium Script

| By Webner

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.

Elements in Selenium Script

Leave a Reply

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