Overview

Selectors are at the core of finding elements before you can interact with them or assert using them. Nightwatch supports the following selectors strategies, which can be used to locate your elements:

  • id
  • xpath

ID

This is the native element id i.e. resource-id on Android & name for iOS . Even if you don’t have access to the code base, you can very easily find the ID using the Appium Inspector tool.

Example of ID selector

As you can see the highlighted element has an id value org.wikipedia:id/search_src_text.

To interact with that element, selector strategy would be id and selector string would be org.wikipedia:id/search_src_text.

XPath

Apart from ID, you can also locate an element using the XPath.

Example of xPath selector

Xpath in Appium analyzes the XML structure of the app and then locates the element. Xpath should only be used when there is no ID, Name, or accessibility ID assigned to a specific UI element. Although XPath allows for the formulation of complex queries, using XPath is not recommended because it has stability and performance issues (as mentioned in the official documentation).

Selecting nth element

Nightwatch selectors also accept a selector object instead of a string where more options can be provided. You can select an element at nth index from a list of elements. For e.g. in case you have a list with multiple list items, you can select the nth element by sending a selector object instead of a selector string.

Select nth element
app.click({selector: 'org.wikipedia:id/page_list_item_title', locateStrategy: 'id', index: n})

Now that you understand selectors, you can use them to write commands & assertions.

Command
Assertions