Quantcast
Viewing all articles
Browse latest Browse all 11

Reach parent of an element using WebDriver

Sometimes it is required to reach at the parent location of an element. I faced a  problem while trying to reach at the parent locator from the child locator. I explored a lot , discussed with my Selenium team mates for the same and finally found a solution which is very easy to handle. I wanted to share the piece of information to all of my selenium fellows so that they would not face the same problem. It is very easy to reach at the parent location by using xpath only. We need only the locator of the child element from where we want to back. You can use the below piece of code for the above situation.

Code:

try{
driver.findElement(childLocator).findElement(By.xpath(".."));
}catch(Throwable e){
system.out.println("Error reaching the parent locator"+e.getMessage());
}

Here childLocator is the location of the child element from where you want to back

So our problem is solved!!! 

Written By: Sudhansu Sekhar Panda, QA Engineer, Mindfire Solutions


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 11

Trending Articles