Tips for selecting elements reliably (XPath and Accessibility ID)

Prev Next

If Autify cannot reliably select the right element via image recognition, using XPath or Accessibility ID may help. In this section, we will show you how to select an element using XPath and Accessibility ID.

XPath

If you chose XPath as the locator, Autify will automatically generate an XPath.
The auto-generated XPath only includes the element type and its display order. You can improve reliability by supplying additional information or removing unnecessary information from the XPath.

Example:

Type Example of XPath Feature
Auto-generated /XCUIElementTypeApplication/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/<>XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[2]/XCUIElementTypeOther[1]/XCUIElementTypeOther/XCUIElementTypeSecureTextField It refers to the entire tree of the element, so changes that are not directly related to the element may have an effect. It only looks at the type and order of the element, so the wrong element is selected if the order is changed.
After manual adjustment //XCUIElementTypeCell[contains(@value, “Password”)]//XCUIElementTypeSecureTextField Only includes information that is important for locating the element, which improves stability. Learning requires some trial and error.

Below are examples of commonly used XPaths:

Using attributes

On the Quick Element Update page, if the target element or its parent-child elements have distinctive attributes, including them in the XPath can improve reliability. Here are examples that use the value attribute.

XPath Element to be selected
//XCUIElementTypeButton[@value="OK"] Button whose value is "OK"
//*[contains(@name, ‘limited’)] Element whose name includes "limited"
//*[starts-with(@name, "2024")] Element whose name starts with "2024"
//*[ends-with(@name, "login")] Element whose name ends with "login"
//*[@name="Return"] Element whose name is "Return"
(//*[@name="Done" or "完了"]) Element whose name is "Done" or "完了"

Specifying the order

When multiple elements match the XPath, you can select an element by its order.

XPath Element to be selected
//XCUIElementTypeButton[3] The third button
//XCUIElementTypeTextField[last()] The last text field

Composite locator

If the element you want to select lacks distinctive characteristics, you can select it based on its relative relationship with other elements.

XPath Element to be selected
//*[@value=”register”]//XCUIElementTypeButton Button within an element whose value is "register"
//*[value=”Check out”] ~ XCUIElementTypeButton Button adjacent to an element whose value is "Check out"
//[@name=”List”]/[5] Fifth element directly under an element whose name is "List"

Accessibility ID

If Autify cannot select an element using other methods, we recommend setting up Accessibility ID.

If the target element has the "name" attribute for iOS or the "content-desc" attribute for Android, the target element can be selected by setting the attribute value as the Accessibility ID on the Quick Element Update page.

If the target element does not have an Accessibility ID, you need to modify the application code.

Setting up Accessibility ID

iOS Native

Add the accessibilityIdentifier property to the target element.

Android Native

Add contentDescription to the target element.

Flutter

(iOS) Set the Semantics identifier property for the target element.

(Android) Currently unavailable. If you set the Semantics identifier property for an element, it will appear as a property called “resource-id” in the page source, so you can use it in an XPath.

ReactNative

(iOS) Set the testID property for the target element.

(Android) Currently unavailable. If you set the testID property for an element, it will appear as an attribute called “resource-id” in the page source, so you can use it in an XPath.