AppiumBy Interface
AppiumBy Interface
The AppiumBy interface is part of the Appium Java client library and provides a convenient way to locate elements in mobile automation tests using Appium. It extends the By class from Selenium and introduces mobile-specific locator strategies that are not available in standard Selenium.
- Package:
io.appium.java_client - Purpose: Offers static factory methods to create
Bylocators tailored for iOS, Android, and cross-platform mobile automation. - Replaces deprecated methods: Older locator strategies like
By.id(),By.xpath(), etc., still work, butAppiumByprovides explicit, semantic methods for mobile contexts.
📦 Dependency (Maven)
Make sure you’re using a recent version of the Appium Java client (v8+):
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.2.3</version> <!-- Check for latest version -->
</dependency>
Note: As of Appium Java Client v8+, many old locators (like MobileBy) are deprecated in favor of AppiumBy.
âś… Common Locator Strategies in AppiumBy
| Method | Description | Example |
|---|---|---|
id(String id) |
Finds element by resource-id (Android) or name/accessibility id (iOS) | AppiumBy.id("loginButton") |
accessibilityId(String id) |
Uses accessibility ID (iOS) or content-desc (Android) | AppiumBy.accessibilityId("Login") |
xpath(String xpath) |
Standard XPath locator | AppiumBy.xpath("//android.widget.Button[@text='Submit']") |
className(String className) |
Locates by UI element class | AppiumBy.className("android.widget.EditText") |
androidUIAutomator(String uiautomatorText) |
Android-only: Uses UIAutomator selector | AppiumBy.androidUIAutomator("new UiSelector().text(\"OK\")") |
iOSNsPredicateString(String predicate) |
iOS-only: Uses NSPredicate | AppiumBy.iOSNsPredicateString("label == 'Cancel'") |
iOSClassChain(String classChain) |
iOS-only: Uses class chain (for XCUITest) | AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`label == \"Save\"`]") |
image(String b64Image) |
Locates element by image (requires image comparison enabled) | AppiumBy.image(encodedImageString) |
custom(String selector, String args) |
For custom locator plugins | AppiumBy.custom("plugin-name", "args") |
đź’ˇ Example Usage
import io.appium.java_client.AppiumBy;
import org.openqa.selenium.WebElement;
// Find by accessibility ID (works on both platforms)
WebElement loginBtn = driver.findElement(AppiumBy.accessibilityId("Login"));
// Android-specific: UIAutomator
WebElement okButton = driver.findElement(
AppiumBy.androidUIAutomator("new UiSelector().text(\"OK\")")
);
// iOS-specific: NSPredicate
WebElement cancelButton = driver.findElement(
AppiumBy.iOSNsPredicateString("label == 'Cancel'")
);