Vincent
Vincent

Reputation: 273

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?

Upvotes: 18

Views: 39390

Answers (2)

Vilas Kolhe
Vilas Kolhe

Reputation: 81

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

X will be the Xth sibling of that element.

Upvotes: 8

har07
har07

Reputation: 89285

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

Upvotes: 43

Related Questions