Appium: Find element by partial elementId/UID with XPath

Viewed 2187

I am working on a test automation for an iOS app. I use the Appium-desktop-client (1.15.1) on MacOS (10.15) and find an element on Iphone 11 (13.3) via the Inspector with the following attributes: screenshot from the attributes

The goal is to do the following in java with appium (6.1.0) and selenium (3.13.0):

Find this element via the elementId and get the variable value (in this case 2,6 km).

The problem is reaching the element. In the Inspector, the accessibility-id and the name are always equal to the value, which means that I cannot localize the element with them. The elementId is not fixed, it varies after the 3rd block of numbers (that means I can only use the 45000000-0000-0000.) I can also reproduce this elementId from the Inspector using the code from the "UID" attribute.

To localize the element I tried the following:

appiumDriver.findElementByXPath("//*[contains(@id,'45000000-0000-0000')]");

appiumDriver.findElementByXPath("//*[contains(@name,'45000000-0000-0000')]");

appiumDriver.findElementByXPath("//*[contains(@uid,'45000000-0000-0000')]");

appiumDriver.findElementByXPath("//*[contains(@elementId,'45000000-0000-0000')]");

appiumDriver.findElementByXPath("//*[contains(@RuntimeId,'45000000-0000-0000')]");

appiumDriver.findElementByXPath("//*[contains(@ProcessId,'45000000-0000-0000')]");

appiumDriver.findElementByXPath("//*[contains(@class,'45000000-0000-0000')]");

However, with all these attempts to locate the element came an org.openqa.selenium.NoSuchElementException.

My question now is, what do I have to write in the xpath to find the element via the part of the elementId from the Inspector (or the attribute UID) and then be able to read out the value.

1 Answers

I would try this secure option :

//*[local-name(@*)="@elementId"][starts-with(normalize-space(string(@*)),"45000000-0000-0000")]

EDIT : contains option doesn't work because the elementId is not the first attribute of the element.

Could you try :

//*[@*="45000000"]

If this doesn't work let's bruteforce this with :

//*[contains(@*[2],'45000000')]
//*[contains(@*[3],'45000000')]
//*[contains(@*[4],'45000000')]
//*[contains(@*[5],'45000000')]
//*[contains(@*[6],'45000000')]
//*[contains(@*[7],'45000000')]
//*[contains(@*[8],'45000000')]
//*[contains(@*[9],'45000000')]
//*[contains(@*[10],'45000000')]
//*[contains(@*[11],'45000000')]
Related