Finding elements on a phone

A phone block that acts on an element — a button, a field, a list item — finds it by a selector over the screen's elements, not by where it happened to be. A flow written that way keeps working when a banner pushes a button down or the phone has another screen size.

Selectors are XPath over the screen

The screen is a tree of elements named by their class, such as android.widget.Button, each with the attributes Android reports: resource-id, text, content-desc, class, package, bounds, index and true/false flags such as clickable. A selector is XPath 1.0 over that tree — the same as Appium Inspector writes, so one copied from Appium works here.

The Selector field takes three things, told apart by how they start: text starting with / or (/ is an XPath; text starting with { or [ is the Inspector's own locator, which keeps several ways of finding the element and tries the next when one stops matching; anything else is an element's exact text, such as Sign in.

You writeIt finds
//*[@resource-id='com.example.shop:id/sign_in']The element with that id — the sturdiest choice when the app gives one.
//android.widget.Button[@text='Sign in']A button whose text is exactly “Sign in”.
//*[@content-desc='Search']An icon button by its accessibility label.
//*[contains(@text,'Order #')]Any element whose text contains “Order #”.
(//android.widget.EditText)[2]The second text field on the screen.

Inspect: pick it on the screen

  1. Pick the phone in the editor's toolbar and open the app on the screen you want.
  2. In a block with a selector, choose Inspect. The Inspector opens with the phone's screen, its element tree, and the element under your pointer outlined.
  3. Click the element. Selected element shows its attributes, and Locator lists the selectors that name it alone, best first — by id, by its accessibility label, by its text, by a child's text, by an anchor beside it, or by position. A higher score keeps working through more app updates.
  4. Use in block puts the Inspector's locator into the block's Selector; Use XPath puts the XPath instead, if you would rather read and edit it.
Picking Sign in in the Inspector: the outlined element, and the selector it suggests first.

When the Inspector says No selector names this element alone., the element has nothing that sets it apart — pick its parent or a neighbour with text, or use a picture (below).

Verify: check it on the phone now

Verify, beside Inspect, runs the selector against the phone's screen as it is and says what it finds. Use it after editing a selector by hand.

When there is no element: pictures and text

Games, maps and some web views draw everything as one picture, with no elements inside. In the Inspector, draw a box around what you want:

  • Find it by its picture crops it for Image search, which finds the same picture on the screen at run time.
  • Read the text in it sets up Read text (OCR) for that area; Find text (OCR) finds a word anywhere on the screen by reading it.

Points

Touch and Swipe / Scroll can also take a point: the Inspector says This block takes a point: click once on the picture. Points are kept as shares of the screen, so they land in the same place on another resolution — but not if the app lays that screen out differently. Prefer an element when there is one.

An element further down

Element action can scroll to look for an element that is not on screen yet, and Wait for element waits for one an app is still loading.