Getting next sibling element using XPath and Selenium for Java
31,265
Solution 1
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
Solution 2
WebElement parent = child.findElement(By.xpath("following-sibling::*[X]"));
X will be the Xth sibling of that element.
Author by
Vincent
Updated on July 09, 2022Comments
-
Vincent almost 2 years
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?
-
Lingamurthy CS about 9 yearsAnd to get only the next sibling, use
following-sibling::*[1]
. -
Heinz about 5 yearsShould be Xth?
-
Sam Ginrich about 2 yearswill this quite probably deliver a text element?