top button

How would you count the number of elements on a page?

0 votes
posted Apr 26 by Arun

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes

This is a common selenium interview question because in many cases you want to click on an item from a list. So it is important to know how to count the elements and select the correct one from the list.

We first need to locate the node element where the items are listed. For example, in the HTML code below:

  • movie title 1
  • movie title 2
  • . . .
  • movie title 50

The root element can be located using

List movies = driver.findElements(By.cssSelector(ul#movies li));

Then we can use the .size() to get the number of


int numberOfMovies = movies.size();

answer Apr 29 by Arunkumaarts