1

I need to know how to store the first 10 values in List<WebElement> instead of storing all the elements that is present?

Right now my code stores all the elements:

By mySelector = By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li");
List<WebElement> myElements = driver.findElements(mySelector);
for(WebElement e : myElements) {
  System.out.println(e.getText());
}
John
  • 53
  • 1
  • 7

1 Answers1

4

Try using position() with the li.

By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li[position() < 11]")
Grasshopper
  • 8,908
  • 2
  • 17
  • 32