28 / 66 · 09 Mobile & Cross-Platform Testing · Appium Fundamentals← prev⊞ allnext →☰ Read as one page
5.4Element Location Strategies
Choosing the right locator strategy is critical for test stability:
| Strategy | Syntax | Stability | Speed | When to Use |
|---|---|---|---|---|
ACCESSIBILITY_ID |
AppiumBy.ACCESSIBILITY_ID |
Best | Fast | Default choice -- stable across layouts |
ID |
AppiumBy.ID |
Good | Fast | When accessibility ID is not set |
CLASS_NAME |
AppiumBy.CLASS_NAME |
Low | Medium | Only with additional filtering |
XPATH |
AppiumBy.XPATH |
Worst | Slow | Last resort -- fragile, slow |
IMAGE |
AppiumBy.IMAGE |
Good | Medium | Visual element matching |
Best Practice: Always Use Accessibility IDs
# GOOD: Accessibility IDs are stable and meaningful
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "checkout-button")
# BAD: XPath is fragile and slow
driver.find_element(
AppiumBy.XPATH,
"//android.widget.LinearLayout[3]/android.widget.Button[2]"
)
# BAD: Resource IDs are platform-specific and can change
driver.find_element(
AppiumBy.ID,
"com.myapp:id/btn_checkout_v2_redesign_final"
)
Accessibility IDs have a double benefit: they make your tests stable AND they improve your app's accessibility. Every testable element should have an accessibility label.