0 votes
1 view
in DevOps and Agile by (19.8k points)

I have a certain element that I can select with Selenium 1.

Unfortunately, I need to click the parent element to get the desired behavior. The element I can easily locate has an attribute unselectable, making it dead for clicking. How do I navigate upwards with XPath?

1 Answer

0 votes
by (63.4k points)

There are a couple of options there. The sample code is in Java, but a port to other languages should be straightforward.

WebElement myElement = driver.findElement(By.id("myDiv"));

WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript(
                                   "return arguments[0];", myElement);
XPath:
WebElement myElement = driver.findElement(By.id("myDiv"));
WebElement parent = myElement.findElement(By.xpath("./.."));

Obtaining the driver from the WebElement
Note: As you can see, for the JavaScript version you'll need the driver. If you don't have direct access to it, you can retrieve it from the WebElement using:

WebDriver driver = ((WrapsDriver) myElement).getWrappedDriver();

Hope this helps! 

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium automation certification!

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...