Cannot find an element with Xpath in Appium
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
I tried the following Xpath options:
- From the AndroidDriver level:
driver.findElement (By.xpath ("//*[@id='read_indicator']")); driver.findElement (By.xpath ("//android.widget.ImageView[@id='read_indicator']"));
- 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']"));
- The same methods as above but starting from a frame layout with ID
footer_delivery_statusthat 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
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
Thread.sleepwhere it is needed, so the elements should be present.
Please advice why I have such issues with Xpath.
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.
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.