Appium is selecting the wrong locator

Viewed 575

I am testing a sample Android app on an Android 6.0 phone and running Appium UI version 1.10.0 and java version "1.8.0_111". I need to click on the Office Location menu item but I am unable to find a suitable locator.

Here are the snapshots from Appium Inspector.

Appium Inspector 1

I tried using the recording feature in Appium Inspector as well which does send me to the correct screen on the app.

Appium Inspector 2

However, when I use the following line of code in my project, the incorrect menu item is selected.

MobileElement el1 = (MobileElement) driver.findElementByXPath("(//android.widget.ImageView[@content-desc=\"73: [Office Locations]\"])[2]");
el1.click();

I would expect the Office Locations menu item to be selected, instead, a different menu item with a different cont_desc is being selected.

Any suggestions?

3 Answers

First of all try to find same xpath in appium inspector and check how many matching elements it is showing.

If it shows only one matching element then try to perform some action on found element and check where it is performing action.

Also try to use resource-id to find the element. If not present for this element then use its parent element or sibling elements.

Many a time people mistakenly get the xpath of element behind actual element and face this kind of problem. Try to avoid generated xpath.

You can access your element by using findElementByAccessibilityId that has cont-desc.

MobileElement element= driver.findElementByAccessibilityId("\"73: [Office Locations]\"");
element.click();

you can ask your developer to add unique cont-desc in the code.

you can find the element by using xpath and in xpath you need to specify the content description i think this might help

driver.findElementByXPath("//android.view.View[@content-desc='description'].click();

you have specified in wrong way hence its not clicking the content description showing as it is you have to mentioned it

Related