从appium中的子元素获取父元素

Get Parent element from child element in appium

我有回收站观点。所有行都具有相同的 ID。每行都有复选框和文本,这些元素也有相同的 ID。但是有些行没有复选​​框。

所以我想做的是通过 id 定位复选框,然后获取其父节点并定位其子节点。

MobileElement childElement= driver.findElement(By.id("checkbox-child"));
MobileElement parentElement= // need to know how to do it 

parentElement.findElement(By.id("text-child")).getText();

所以我想知道如何从子元素中获取父元素。

我能想到的一种解决方法的叠加是:

List<WebElement> listParent = driver.findElements(By.id("parentID")); //Requires parent's resource-id.
for(int i=0;i<listParent.size();i++){
    String parentXPath = "//android.widget.RelativeLayout["+i+"]"
    String childXPath = parentXPath + "//android.widget.Checkbox"; //should get you the child corresponding to the specific Relative Layout
    MobileElement parentElement= driver.findElement(By.XPath(parentXPath); 
    if(isElementPresent(By.XPath(childXPath))){
       parentElement.findElement(By.id("text-child")).getText();
    }

我也会试试这个。