krishworkstech.com

Part 9: Element Locator Strategies in Android (ID, XPath, etc.)

Part 9: Element Locator Strategies in Android (ID, XPath, etc.)

Understanding how to locate elements on a mobile screen is one of the most critical skills in mobile test automation. You’ll use locators to interact with buttons, input fields, switches, popups, and more.

Let’s learn how to identify elements properly and which locator strategies work best with Appium for Android.

Element Locator
Element Locator-1

What Are Element Locators?

Locators help Appium find and interact with elements in your app’s UI — like buttons, text fields, etc.

You can think of them as:

“How do I tell Appium which element to click, type into, or check?”

Tools to Help You Find Locators

1. Appium Inspector

  • UI analysis tool provided with Appium Desktop
  • Shows element properties (resource-id, class, bounds, etc.)
  • Allows live inspection of the app running on your emulator/device

2. UIAutomatorViewer

  • Comes with Android SDK
  • Static tool to inspect elements from a snapshot of your device screen

Both tools give you information like:

  • resource-id
  • text
  • class
  • content-desc (accessibility ID)
  • bounds
  • index
UIAutomatorViewer
UIAutomatorViewer-1

Common Locator Strategies in Android

Here are the most widely used strategies (and when to use them):

1. By ID (resource-id) — Best Choice

				
					driver.findElement(By.id("com.example:id/loginButton")).click();
				
			
  • Fast and reliable
  • Unique most of the time
  • Recommended as first option
    Use only when the available resource id matches the requirement to automate

2. By Accessibility ID (content-desc)

				
					driver.findElementByAccessibilityId("Login").click();
				
			
  • Works well with buttons, icons
  • Useful for cross-platform tests
    Often used for VoiceOver and TalkBack users (accessibility)

3. By XPath — Use with caution

				
					driver.findElement(By.xpath("//android.widget.TextView[@text='Login']")).click();
				
			
  • Flexible (can target almost anything)
  • Slower than ID or Accessibility ID
  • Can break easily if UI changes

Use XPath only when no ID or content-desc is available.

4. By Class Name

				
					driver.findElement(By.className("android.widget.EditText")).sendKeys("admin");
				
			
  • Good for getting all elements of a type
  • Not always unique
  • Avoid using it to find individual elements

5. Android UIAutomator (advanced)

				
					driver.findElementByAndroidUIAutomator("new UiSelector().text(\"Login\")");
				
			
  • Useful for scrolling, filtering, chaining
  • Very powerful for complex screens

Example Using Multiple Strategies

				
					// By ID
driver.findElement(By.id("com.example:id/username")).sendKeys("admin");

// By Accessibility ID
driver.findElementByAccessibilityId("Login").click();

// By XPath (if no ID or accessibility ID)
driver.findElement(By.xpath("//android.widget.Button[@text='Submit']")).click();


// By Android UIAutomator
driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.example:id/password\")").sendKeys("1234");

				
			

Tips to Choose the Best Locator

Use Case

Best Locator

Unique input field or button

				
					By.id()
				
			

Icon or image with a description

				
					By.accessibilityId()
				
			

No ID or accessibility

				
					By.xpath() (last resort)
				
			

Lists, scrolls, or filtering

				
					UiAutomator()
				
			

How to Verify Your Locators Work

Use Appium Inspector to:

  1. Start your Appium server.
  2. Launch the app on an emulator or device.
  3. Use Appium Inspector to click on elements and view their properties.
  4. Copy the locator and use it in your script.

Summary Table

Feature

UiAutomator2

Selendroid

				
					By.id
				
			
				
					By.id("com.example:id/btnLogin")
				
			

Unique elements

				
					By.accessibilityId
				
			
				
					ByAccessibilityId("Login")
				
			

Buttons, icons

				
					By.xpath
				
			
				
					By.xpath("//android.widget.TextView[@text='Submit'])"
				
			

When no ID or accessibility ID

				
					By.className
				
			
				
					By.className("android.widget.EditText")
				
			

Groups of elements

				
					UiAutomator
				
			
				
					UiSelector().text("Submit")
				
			

Advanced targeting, scrolling

What’s Next?

Let’s now move on to Part 10: Page Object Model (POM) Design — where you’ll learn how to organize your locators and actions cleanly for reusable, scalable automation.

Here you will learn how to frame the design and how the frame work can be designed, The framework is designed to match the test suite and

Building complex IoT systems?

Accelerate testing and deployment with our QEMU-integrated toolchain expertise.

Mastering Appium: A Complete Beginner-to-Expert Mobile Automation Guide

image (5) (1)
COVER 3

Part 2: Appium Architecture, Tools Setup & How Test Code Connects to Devices

COVER - PART 4

Part 3: Java Fundamentals for Test Automation

Group 1707478330
COVER 3

Part 5: Setting Up the Environment (Windows & macOS)

Group 1707478196 (1) (1)

Part 6: Understanding UiAutomator2 (Android Engine)

Group 1707478196 (1)
COVER 3

Part 8: Handling Waits and Synchronization in Appium (Android)

COVER - PART 4

Part 9: Element Locator Strategies in Android (ID, XPath, etc.)

COVER (1) (2)
COVER 3

Part 8: Handling Waits and Synchronization in Appium (Android)

COVER - PART 4

Part 9: Element Locator Strategies in Android (ID, XPath, etc.)

Scroll to Top
  • Schematic design
  • PCB and schematic source files
  • Assembling drawing files
  • Providing prototype/sample and production PCB service
  • Testing and validation of designed hardware
  • HIPAA
  • Azure Key
  • Management
  • ES, Checksum,
  • MD5sum
  • AWS
  • Azure
  • GCP
  • DigitalOcean
  • Kotlin
  • Python
  • Tensorflow
  • Computer Vision
  • ECG
  • SPO2
  • Heart Rate
  • Glucometer
  • Blood Pressure
  • UX UI Process
  • Figma and FigJam
  • Adobe Suite
  • Selenium Java
  • Postman
  • Swagger
  • Jmeter
  • SQL
  • Java Scripter
  • Test ng
  • Extents Reports
  • Flutter
  • Java
  • Kotlin
  • Swift
  • Dart
  • React JS
  • Python
  • NodeJS
  • Django
  • HTML, CSS, JS
RDBMS
  • PostgreSQL
  • Oracle
  • MySQL
  • MariaDB
No SQL Based
  • MongoDB
  • GCP
  • FirestoreDB
  • DynamoDB
  • Azure
  • CosmosDB
  • AWS