简体   繁体   中英

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 ("automationName", "UiAutomator2") , Android 8.1 on emulator.

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.

在此处输入图像描述

Try the following xpath :

//*[contains(@resource-id, 'read_indicator')]

Or:

//*[contains(@content-desc, 'Message read')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM