Search code examples
javaandroidseleniumxpathappium

Appium Finding Wrong Element when Finding With xPath


I am working on a development script using Selenium and Appium and I'm running into the issue of the wrong element being picked up by the Selenium Locator.

Essentially, I want to click a button that has no ID assigned to it; so the only thing I have left to identify it by is its text.

public Element button1(){
    By locator = By.xpath("//android.widget.TextView[@text='button1']");
    return new xElement(driver.findElement(locator), locator);
}

This is my my locator method to get the button1 object. By the way, no other button on the screen has text anywhere close to button1's text. The method click called on the button has the format:

public void clickBtn1(){
    button1().click();
}

The button being clicked essentially has the text "wheelbarrow". This is just to clarify that the button being pressed has text no where close to button1's value.

I have used UI automator multiple times to confirm button1's actual text value. The weird thing is the script works occasionally, so I'm not sure what the issue is.

I have also tried a "wait for enabled" method to account for race conditions.


Solution

  • Try using the Appium inspector to search for your button. You can type in the xpath and search for the element to see what it finds. The other nice thing about the inspector is you can see how the native control attributes map to the Appium attributes. 'text' may not be the attribute you actually want. Also, have you tried searching on properties on the Button itself (instead of TextView)?