-1

I use selenium and xpath selector. I know how to get a certain element. But how to use xpath to get each element 5?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user6355594
  • 1
  • 1
  • 1
  • 2

1 Answers1

1

You can always solve it in your language of choice.

Python

For example, in Python, the extended slicing allows to do it rather simply:

driver.find_elements_by_xpath("//table/tr")[0::5]

You can also use the position() function and the mod operator:

//table/tr[position() mod 5 = 0]

driver.find_elements_by_xpath("//table/tr[position() mod 5 = 0]")

Java

List<WebElement> elements=driver.findElements(By.xpath("//tbody/tr[position() mod 5 = 0]"));
System.out.println(elements.size());
for (WebElement element : elements) {
    System.out.println(element.getText());
}
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195