I use selenium and xpath selector. I know how to get a certain element. But how to use xpath to get each element 5?
Asked
Active
Viewed 144 times
-1
-
Could please provide some scenario like HTML code and all?? and mention the programming language as well – Saurabh Gaur Jul 19 '16 at 11:55
1 Answers
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());
}