Appium, Android - Finding element inside another e

2019-06-01 05:28发布

问题:

I am trying to check if element exist, but appium seems to ignore searching an element when we specified that it should be inside another element. For example:

driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");

It should work somehow since there is option in Appium inspector called Locator where after selecting strategy and choosing search from selected element option it finds what I expect.

What is the proper way of finding such elements ?

回答1:

You can use a xpath for the same. Sample xpath for the situation will be like :

driver.findElement(By.xpath("//*[@accessibility-id='FirstElement']//*[@accessibility-id='FirstElement']"))

The // between first element and second element indicates first element is the base element for start searching the second element. So it will search for the second element between the scope of the first element



回答2:

Simple solution but not the cleanest below:

I am using TestNG framework which I suppose do not allow declarations such as

private WebElement element= driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");

Simple solution but not the best is to declare only parents as:

@FindBy(name="Parent")
private WebElement ParentElement;

Then in test case for example:

ParentElement.findElement(By.name("childElement")).isDisplayed())


回答3:

Put the first element as parent and second element as child -

 MobileElement parentButtonList = (MobileElement)driver.findElementById(parentClassIdentifier);
List<MobileElement> childButtonList = parentButtonList.findElementsById(childClassIdentifier);