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.

Share:
31,265
Vincent
Author by

Vincent

Updated on July 09, 2022

Comments

  • Vincent
    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
    Lingamurthy CS about 9 years
    And to get only the next sibling, use following-sibling::*[1].
  • Heinz
    Heinz about 5 years
    Should be Xth?
  • Sam Ginrich
    Sam Ginrich about 2 years
    will this quite probably deliver a text element?