Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting next sibling element using XPath and Selenium for Java

I know that you can get a parent element when using Selenium for Java and XPath like this:

WebElement parent = child.findElement(By.xpath(".."));

But how do I get the next sibling of an element, what do I need to put between the parentheses instead of two dots as in the case above?

like image 754
Vincent Avatar asked May 22 '15 23:05

Vincent


People also ask

How would you get to the next sibling in Selenium?

We can find a next sibling element from the same parent in Selenium webdriver. This is achieved with the help of xpath locator. It is important to note that it is only possible to traverse from current sibling to the next sibling with the help of xpath.


2 Answers

Use following-sibling axis :

WebElement followingSibling = child.findElement(By.xpath("following-sibling::*"));

List of available axes by MDN, for further reference : Mozilla Developer Network : Axes

like image 103
har07 Avatar answered Sep 20 '22 14:09

har07


WebElement parent = child.findElement(By.xpath("following-sibling::*[X]"));

X will be the Xth sibling of that element.

like image 40
Vilas Kolhe Avatar answered Sep 17 '22 14:09

Vilas Kolhe