Get the Parent Node of the Current Selenium WebElement

There are times that the parent is required from the child element.

There are two options JavaScript or XPath:

WebElement myElement = driver  
WebElement parent = (WebElement) ((JavascriptExecutor) driver)  
        "return arguments[0].parentNode;", myElement);
WebElement myElement = driver.findElement("myDiv"));  
WebElement parent = myElement.findElement(By.xpath(".."));  

But what if the parent of the parent is required?

The JavaScript would be

return arguments[0].parentNode.parentNode;  

and the XPath will be


comments powered by Disqus