Tips for selecting elements reliably (XPath and Accessibility ID)
  • 14 May 2024
  • 2 Minutes to read
  • Contributors
  • Dark
    Light

Tips for selecting elements reliably (XPath and Accessibility ID)

  • Dark
    Light

Article summary

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:

TypeExample of XPathFeature
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/XCUIElementTypeSecureTextFieldIt 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”)]//XCUIElementTypeSecureTextFieldOnly 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.

XPathElement 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"

Specifying the order

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

XPathElement 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.

XPathElement to be selected
//*[@value=”register”]//XCUIElementTypeButtonButton within an element whose value is "register"
//*[value=”Check out”] ~ XCUIElementTypeButtonButton 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.


Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.