Skip to main content

AS - XPath Feature

info

Please note that this tool is classified as a Legacy tool. We recommend transitioning to our updated solutions to maintain optimal performance and security in your workflows. For more information on this matter, please reach out to technical support .

When using the object spy to inspect the Native or Web elements of a specific page, the element tree is a representation of an XML file, with each element representing a node with different properties in the tree module. Studio gives the user the option of using XPath 1.0 language syntax in order to identify elements in the tree.

XPath is a powerful query language for XML trees, which provides the ability to use multiple properties of both the element you are looking for and the elements around it (along with their relative location in the tree) in order to be able to always get the identification you are looking for throughout the automation scripts.

To learn more about the XPath language and how to use its syntax, visit w3schools.

To use the XPath feature:

  1. Prepare, install, and launch the Application:
  2. Use the Spy icon button to get the Native OR Web properties of all the objects on the screen. The easiest way to create a simple XPath query is by marking the wanted properties of an element (one or multiple properties can be used), right clicking on them and then clicking on Copy XPath. This copies to the clipboard a query that looks for elements that have the properties and values that are looked for. This example creates a query that looks for elements that have both id property with IMDB_branding as the value and class property with android.widget.ImageView as the value.

The XPath search box on the bottom of the tree window provides the ability to dynamically create and edit a query. The best way to check the elements a query identifies is by inserting the query into that search box. Only elements that meet the query are highlighted on the device's reflection once a query is inserted in the search box.

In this example, the only element that is left highlighted is the one extracted the properties from and wanted to identify. That means that the query gives unique identification of the desired element, and can be safely used in the automation scripts. However, that is not always the case.

In this query, you extracted just the id property of one of the elements (with value label), and when checking the query in the search box, you see that there are 11 other elements on the screen that meet the query and are identified by it. That means that using this query in the automation script can be dangerous because any one of the elements can be identified when this query is called during an action command. That is why it is always recommended to try and create a query that gives unique identification of the element you are looking for.

Continuous Testing also has a feature that will automatically generate a unique XPath query for a specific element.

If a unique query still cannot be generated, it is possible to use the index parameter of the action commands to differentiate between the elements identified by the query.

Advanced XPath Options

Integrate With Relative Elements

  • ../* - Can be used to integrate the properties of a sibling of an element in the query. Each instance of ../ goes up the hierarchy another level, so use ../../* to integrate a property of the father of an element.

    Example: As mentioned, the XPath language gives the ability to use properties of other elements in order to identify the element we are looking for. The following example uses the text property of one of the labels in order to uniquely identify the arrow button to the right of it.
    xpath=//*[@id='accessory']

  • Using just the id property with an accessory label is not a unique identifier, as all 4 arrow buttons meet this query. Notice that there are no other properties for the arrow button element itself that can help to separate it from the others, so use the text property of the label itself in order to get the unique identification. In order to do that, add to the query another part using ../*, that also eliminates all elements that don't have a sibling in the tree with a text property and US TV Recaps value. The arrow button  is now be uniquely identified.
    xpath=//*[@id='accessory' and ../*[@text='US TV Recaps']]

For more information about XPath, see XPath Operators & Functions.