Pages

Tuesday 8 October 2013

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 occured while reaching to the parent location"+e.getMessage());

}

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

So our problem solved!!!