John
John

Reputation: 847

appium - selecting the parent node

I'm currently in a situation where I need to select the parent from a child. The child has an id but the parent doesn't. So this is actually one of the few solutions.

WebElement child = driver.findElement(By.id("books"));
System.out.println(child.getAttribute("name")); //prints correct node
WebElement parent = child.findElement(By.xpath(".."));
System.out.println(parent.getAttribute("name")); // error can't find element

Upvotes: 1

Views: 8583

Answers (2)

Ganesh Devaraj
Ganesh Devaraj

Reputation: 39

In Appium, to select immediate parent node, you need to provide /..

E.g. childElement Xpath = //android.widget[@text='name']

Parent Xpath expression: mobiledriver.FindElement(By.xpath("//android.widget[@text='name']/..").click

Upvotes: 4

ievche
ievche

Reputation: 1805

In order to find element based on other element you need to use . at the start of xpath to select current node. This should work for you:

WebElement parent = child.findElement(By.xpath("./.."));

Upvotes: 1

Related Questions