Mobile Element Locators
Locating elements in mobile apps is fundamentally different from web locators. Mobile apps don't have CSS or XPath in the traditional web sense — instead they use platform-specific identifiers from UIAutomator2 and XCUITest. Knowing the right locator strategy for each platform is critical for stable, maintainable mobile tests.
Mobile Locator Strategies by Priority
- Accessibility ID (best — both platforms): Set by developers via contentDescription (Android) or accessibilityIdentifier (iOS). Cross-platform compatible. Example: AppiumBy.ACCESSIBILITY_ID, 'login-button'
- ID / Resource ID (Android): The R.id.name from the layout XML. Format: 'com.package:id/element_name'. Fast and stable.
- XPath (functional but slow): Use only when above strategies fail. Mobile XPath is the same syntax as web but targets native UI hierarchy.
- UIAutomator2 (Android-only): Powerful, more expressive than XPath for Android. 'new UiSelector().text("Login").className("android.widget.Button")'
- iOS Predicate String (iOS-only): Apple's predicate language. label == 'Login' AND enabled == 1. More expressive than XPath.
- iOS Class Chain (iOS-only): **/XCUIElementTypeButton[`label == 'Login'`] — faster than XPath, iOS-specific.
- Image Recognition: Appium can locate elements by image (pixel matching). Use for non-accessible elements only.
Mobile Locator Examples
from appium.webdriver.common.appiumby import AppiumBy
# ── 1. ACCESSIBILITY ID (recommended — works on both platforms) ─
login_btn = driver.find_element(
AppiumBy.ACCESSIBILITY_ID, "login-button"
)
# Android: Requires contentDescription="login-button" in layout XML
# iOS: Requires .accessibilityIdentifier("login-button") in Swift/ObjC
# ── 2. RESOURCE ID (Android only) ─────────────────────────────
email_field = driver.find_element(
AppiumBy.ID, "com.mycompany.myapp:id/email_input"
)
# ── 3. XPATH (cross-platform, use sparingly) ──────────────────
# Android:
login_btn_android = driver.find_element(
AppiumBy.XPATH, '//android.widget.Button[@content-desc="login-button"]'
)
# iOS:
login_btn_ios = driver.find_element(
AppiumBy.XPATH, '//XCUIElementTypeButton[@name="login-button"]'
)
# ── 4. UIAutomator2 (Android only — most powerful) ────────────
# Find button with specific text:
btn = driver.find_element(
AppiumBy.ANDROID_UIAUTOMATOR,
'new UiSelector().text("Sign In").className("android.widget.Button")'
)
# Scroll to element by text (unique to UIAutomator2):
driver.find_element(
AppiumBy.ANDROID_UIAUTOMATOR,
'new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView('
'new UiSelector().text("Forgot Password"))'
)
# ── 5. iOS Predicate String (iOS only) ────────────────────────
login_btn_ios = driver.find_element(
AppiumBy.IOS_PREDICATE, 'label == "Sign In" AND type == "XCUIElementTypeButton"'
)
# ── 6. iOS Class Chain (iOS only — faster than XPath) ─────────
submit = driver.find_element(
AppiumBy.IOS_CLASS_CHAIN,
'**/XCUIElementTypeButton[`label == "Sign In"`]'
)
# ── APPIUM INSPECTOR: Find locators visually ──────────────────
# Appium Inspector is a GUI tool to inspect running apps:
# 1. Start Appium server
# 2. Launch Appium Inspector
# 3. Connect with device capabilities
# 4. Tap elements to see their locator attributes
# Download: github.com/appium/appium-inspector
# PRO TIP: Request developers add accessibility IDs to all
# interactive elements — reduces locator maintenance by 80%Common Mistakes
- Using XPath by element position — //android.widget.Button[2] changes when UI is reordered; always use attribute-based locators
- Not working with developers to add accessibility IDs — the most impactful thing you can do for mobile test stability is getting devs to add testability attributes
- Using Appium Inspector to generate absolute XPath — Inspector's default 'XPath' generation creates brittle absolute paths; manually write attribute-based selectors
- Same locator strategy for Android and iOS — Android uses ID/UIAutomator2; iOS uses Predicate/ClassChain; always platform-switch in your page objects
Tip
Tip
Practice Mobile Element Locators in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Technical diagram.
Practice Task
Note
Practice Task — (1) Write a working example of Mobile Element Locators from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mobile Element Locators is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready software testing code.
Key Takeaways
- Locating elements in mobile apps is fundamentally different from web locators.
- Accessibility ID (best — both platforms): Set by developers via contentDescription (Android) or accessibilityIdentifier (iOS). Cross-platform compatible. Example: AppiumBy.ACCESSIBILITY_ID, 'login-button'
- ID / Resource ID (Android): The R.id.name from the layout XML. Format: 'com.package:id/element_name'. Fast and stable.
- XPath (functional but slow): Use only when above strategies fail. Mobile XPath is the same syntax as web but targets native UI hierarchy.