Appium locators: what to choose and what to avoid
The best mobile locator is the accessibility id: a fast native strategy that does not depend on the layout. It is content-desc on Android and name on iOS, and developers must agree on a non-localised, unique identifier. Next come id and the platform's native strategies. XPath comes last: the platform does not support it, so the driver evaluates it over XML, which is slower and layout-dependent.
Appium · Locators
A test taps the wrong button after a designer added a banner. What was wrong with the locator?
In mobile tests a locator outlives a screen: the UI is redrawn, text is translated, and the test must stay green. So choosing a strategy is a choice between a stable test and one that breaks after every release. This lesson gives an order of choice and a way to check a locator before coding.
How it works and why. The AppiumBy class (since java-client 8; the old MobileBy was removed in version 9.0) adds mobile strategies to the standard By. The driver translates each into a call of the system mechanism: UiAutomator on Android, XCUITest on iOS. Native strategies are executed by the platform itself, so they are fast. The platform cannot do XPath: the driver receives the whole screen hierarchy as XML and evaluates the expression itself. That is why XPath is slower, and the layout, not the meaning, decides its fate.
The order of choice
| Strategy | Android (UiAutomator2) | iOS (XCUITest) |
|---|---|---|
| accessibility id | content-desc | an alias of the name strategy |
| id | resource-id | an alias of the name strategy |
| class name | the element class | the element type (XCUIElementType…) |
| Native | -android uiautomator (UiSelector) | -ios predicate string, -ios class chain |
| xpath | supported, the slowest | supported, the slowest |
- accessibility id:
AppiumBy.accessibilityId("login"). On Android this iscontent-desc, on iOS the strategy is an alias ofname. One value for both platforms, if agreed with developers. A trap: the XCUITest documentation notes thatnameis usually shown in the UI, so it can change with the language; ask for a separate non-localised identifier and check in Appium Inspector what actually landed in the attribute. - id:
AppiumBy.id("com.example:id/login")isresource-idon Android. Fast when developers set unique ids. - -android uiautomator accepts
UiSelectorexpressions, includingUiScrollablefor scrolling. The driver documentation reminds that Google plans to remove these classes, so do not build the whole project on them. - -ios predicate string and -ios class chain are the native XCUITest locators. A predicate expresses conditions on attributes, while a class chain walks the hierarchy quickly.
- XPath last: its success depends on the layout, and on iOS it can be much slower than the native strategies.
Example: before → after. A locator by position:
// fragile: position in the tree
driver.findElement(AppiumBy.xpath(
"//android.widget.LinearLayout[2]/android.widget.Button[1]")).click();
The same through an agreed name, and where there is none, through a native strategy:
// stable: an agreed name
driver.findElement(AppiumBy.accessibilityId("login_button")).click();
// when there is no name: a native strategy instead of XPath
AppiumBy.androidUIAutomator("new UiSelector().text(\"Sign in\")");
AppiumBy.iOSNsPredicateString("label == 'Sign in'");
AppiumBy.iOSClassChain("**/XCUIElementTypeButton[1]");
XPath and accessibility id
By position in the tree
A layout change, a new wrapper or a different button order breaks the test, and the driver evaluates the expression over the whole screen tree every time.
By an agreed name
Moving the button around the screen does not touch the test: the name belongs to the element, not to a place in the layout.
Scrolling to an element in a long list on Android is also done with a native locator:
// Android: scroll a list to an element
AppiumBy.androidUIAutomator(
"new UiScrollable(new UiSelector().scrollable(true))"
+ ".scrollIntoView(new UiSelector().text(\"Settings\"))");
Appium Inspector is a separate tool that starts a session, shows the element tree with attributes and lets you check a locator before writing code. This quickly shows which value actually sits in content-desc or name and whether it is unique. The Inspector tree also shows what accessibility sees: an element with no name or id is better not worked around with XPath, but a request to developers to add an identifier.
Waiting. Screens load at different speeds, so Thread.sleep gives either a wasted pause or flakiness. Use an explicit wait and do not mix it with an implicit wait: the total time becomes unpredictable.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(
AppiumBy.accessibilityId("home_title")));
// not this: sleep gives either a wasted pause or flakiness
Thread.sleep(5000);
Limits and alternatives.
- Native strategies are tied to a platform, so a shared Page Object needs a separate locator for each. The
@AndroidFindByand@iOSXCUITFindByannotations describe both in one field; an accessibility id reduces the need for two versions. - Agreeing on identifiers is a team practice, not an Appium feature. Without it you have to choose between text that changes on translation and a position that changes on a redesign.
- Text locators (
text,label) break on localisation. They are acceptable if tests run in one language and that is documented. class nameis useful for counting elements of a type, and for finding a single element it is almost always ambiguous.
Typical mistakes.
- Copying an XPath from Inspector as is: it takes a path by position, which breaks first.
- Searching by visible text in an app that is translated.
Thread.sleepinstead of an explicit wait.- Mixing implicit and explicit waits: timeouts add up unpredictably.
- Not agreeing on identifiers with developers and fighting with XPath instead.
Check yourself
Why is accessibility id usually the best choice?
Answer
It is a fast native strategy, and the value does not depend on the position of the element in the layout. The same name can be agreed for both platforms. The key is that it is non-localised and unique.
Why is XPath slower and more fragile than native strategies?
Answer
The platform does not support XPath: the driver receives the whole screen hierarchy as XML and evaluates the expression itself. Also, an expression by position in the tree breaks with any layout change.
What is Appium Inspector for?
Answer
It starts a session, shows the element tree with attributes and lets you check a locator before writing code. It shows what really sits in content-desc or name and whether the value is unique.
AppiumBy provides the mobile strategies: accessibilityId (content-desc on Android), id, native UiSelector, predicate string and class chain. I take XPath last because it is slow and depends on the layout. I check a locator in Appium Inspector and wait explicitly, not with sleep. Middle/Senior: I agree non-localised identifiers with developers so one accessibility id works on both platforms and survives a redesign. I know the documentation warns about dependence on UiSelector, so I do not bake it in everywhere. For a cross-platform Page Object I use annotations for both platforms and look at lookup speed, not only whether the test turned green.