Locator Strategies in Appium Vega Driver
Locator strategies are methods used to find and identify user interface (UI) elements within an app during test automation. These strategies act as search patterns that tell Appium how to locate specific elements on the screen, such as buttons, text fields, or other UI components.
Supported strategies
Appium Vega Driver supports UiSelector and XML Path Language (XPath). Other selector strategies aren’t supported in this release.
UiSelector
A platform-specific locator strategy that leverages native automation frameworks to identify UI elements. It provides precise control over element selection based on properties and hierarchical relationships.
Example usage:
Select a tab to see an example usage.
element = driver.find_element("UiSelector", '{"args": {"text": "Element Text"}}')
element = driver.findElement(AppiumBy.ByAndroidUIAutomator.androidUIAutomator("{\"args\": {\"role\":\"edit\"} }")`\
XPath
A query language that enables navigation and selection of elements in an XML-like structure.
Example usage:
Select a tab to see an example usage.
element = driver.find_element(by="xpath", value="//[contains(@description, \"Music\")]"
element = driver.findElement(AppiumBy.xpath("//[contains(@description, \"Music\")]"));
const element = await driver.$('//[contains(@description, \"Music\")]');
element = @driver.wait { |d| d.find_element :xpath, '//[contains(@description, \"Music\")]' }
Key components
-
Axis - Specifies the direction of the search, such as descendant, ancestor, and following.
// Descendant axis "//button" // All button descendants "/button" // Direct child buttons
-
Node test - Specifies the node type to select, such as element, attribute, or text().
"//*" // Any element "//button" // Button elements "//text()" // Text nodes
-
Predicate - Filters the selected nodes that are based on various conditions, such as attribute values, position, or custom functions.
"//button[@text='Login']" // Exact match "//button[contains(@text, 'Login')]" // Partial match
Special use cases
// Root element
driver.findElement(By.xpath("/root"));
// Elements by description
driver.findElement(By.xpath("//*[contains(@description, 'Music')]"));
// Complex hierarchical selection
driver.findElement(By.xpath("//parent/*[2]//child[@visible='true']"));
Comparison of selector strategies
Feature | UiSelector | XPath |
---|---|---|
Platform support | Platform-specific | Cross-platform |
Performance | Generally faster | Can be slower |
Reliability | More reliable | Might break with UI changes |
Complexity | Moderate | Can be complex |
Maintenance | Easy for platform-specific tests | Better for cross-platform tests |
Debug support | Native tools available | Standard XML tools |
Memory usage | Lower | Higher for complex queries |
Best practices
-
Choose the right strategy
- Use UiSelector for platform-specific tests
- Use XPath for cross-platform compatibility
- Prefer UiSelector when performance is critical
-
Element location
- Use unique identifiers when possible
- Avoid absolute paths
- Keep selectors as simple as possible
- Regularly validate selectors' reliability
-
Performance considerations
- Complex XPath queries may impact test execution time
- Cache frequently used elements
- Use app-specific IDs when available
-
Maintenance tips
- Document locator strategies
- Review locators regularly
- Update with UI changes
- Maintain cross-platform compatibility
- Use meaningful names in test code
Last updated: Sep 30, 2025