Interview Questions

Selenium WebDriver - How to find more than one web element in the list?

Selenium WebDriver Interview question and Answers


(Continued from previous question...)

Selenium WebDriver - How to find more than one web element in the list?

At times, we may come across elements of same type like multiple hyperlinks, images etc arranged in an ordered or unordered list. Thus, it makes absolute sense to deal with such elements by a single piece of code and this can be done using WebElement List.
Sample Code
1 // Storing the list
2 List <WebElement> elementList = driver.findElements(By.xpath("//div[@id='example']//ul//li"));
3 // Fetching the size of the list
4 int listSize = elementList.size();
5 for (int i=0; i<listSize; i++)
6 {
7 // Clicking on each service provider link
8 serviceProviderLinks.get(i).click();
9 // Navigating back to the previous page that stores link to service providers
10 driver.navigate().back();
11 }

(Continued on next question...)

Other Interview Questions