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?
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.
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
WebElement parent = child.findElement(By.xpath("following-sibling::*[X]"));
X will be the Xth sibling of that element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With