0

I'm new to Appium. Sorry if the question is duplicated, but I did not find the solution.

I have a problem - I cannot locate an element using Xpath, but I have no problems locating it using the ID locator. See the screenshot attached. The issue is reproduced not only for this element, but for all. It seems that Xpath does not work at all.

I'm using Appium 7.3.0 (tried different versions), UIAutomator2 by setting the capability ("automationName", "UiAutomator2").

I tried the following Xpath options:

  1. From the AndroidDriver level:
driver.findElement (By.xpath ("//*[@id='read_indicator']"));
driver.findElement (By.xpath ("//android.widget.ImageView[@id='read_indicator']"));
  1. From the message element itself:
MobileElement message = driver.findElement (By.id ("body_bubble"));
message.findElement (By.xpath (".//*[@id='read_indicator']"));
message.findElement (By.xpath (".//android.widget.ImageView[@id='read_indicator']"));
  1. The same methods as above but starting from a frame layout with ID footer_delivery_status that contains the required element directly.

But I can find the element without any difficulties using the ID locator:

driver.findElement (By.id ("read_indicator"));
message.findElement (By.id ("read_indicator"));

Also, functions like contains(), ends-with() etc. do not work too. Throws the following exception:

org.openqa.selenium.InvalidSelectorException: java.lang.IllegalArgumentException: Unable to compile '//*[contains(@id,'read_indicator')'

Also, I'm using implicitWait and Thread.sleep where it is needed, so the elements should be present.

Please advice why I have such issues with Xpath.

enter image description here

2

When querying Android apps with Xpath there is no id attribute. There is a resource-id attribute instead. Appium is doing work behind the scenes to still provide By.id as a locator despite this.

By.xpath("//*[contains(@resource-id, 'read_indicator')]");

If you don't use contains here, you'll need to put in the full resource-id, which is very long.

Your other examples contain compilation errors, and potentially functions that don't exist in XPath 1.0, which I believe is what WebDriver and most browsers are still using. I could not find ends-with() in the spec for 1.0

Be very careful to make sure you close everything that you open.

Unable to compile '//*[contains(@id,'read_indicator')'

The Xpath here cannot compile because it's missing ] at the end.

By.xpath("//*[contains(@id,'read_indicator')]")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.