Fading Coder

One Final Commit for the Last Sprint

Home > Tools > Content

Advanced Element Locating Techniques in Appium UI Automation

Tools May 19 5
  1. Advanced XPath Strategies

XPath in Appium enables powerful element navigation through hierarchical relationships: - Parent-to-child traversal

  • Child-to-parent navigasion using ..
  • Sibling element selection via shared parent
  • Grandparent-to-grandchild pathing

For comprehensive XPath syntax, refer to: W3Schools XPath Tutorial

Example scenario:

  1. Launch Xueqiu app
  2. Tap search field
  3. Input "阿里巴巴"
  4. Locate current price of Alibaba Group (HK stock code: 09988)
def test_stock_price(self):
    # Trigger search
    self.driver.find_element_by_id('com.xueqiu.android:id/tv_search').click()
    
    # Enter search term
    self.driver.find_element_by_id('com.xueqiu.android:id/search_input_text').send_keys('阿里巴巴')
    
    # Select specific search result
    self.driver.find_element_by_xpath(
        "//*[@resource-id='com.xueqiu.android:id/name' and @text='阿里巴巴']"
    ).click()
    
    # Navigate from stock code to price element
    price_locator = (
        MobileBy.XPATH,
        "//*[@text='09988']/../../..//*[@resource-id='com.xueqiu.android:id/current_price']"
    )
    
    # Wait for element visibility
    price_element = WebDriverWait(self.driver, 10).until(
        lambda d: d.find_element(*price_locator)
    )
    
    current_value = float(price_element.text)
    assert current_value > 150.0

  1. UiAutomator Locator Expressions

Appium integrates Android's UiAutomator engine for native element identification. While ofefring superior performance, this string-based approach lacks IDE support and is prone to syntax errors. Key advantages include:

  • Fuzzy text matching capabilities
  • Efficient scrolling operations
  • Complex UI traversal without XPath overhead

Common patterns:

# Scroll to element and click
scroll_command = (
    'new UiScrollable('
        'new UiSelector().scrollable(true).instance(0)'
    ').scrollIntoView('
        'new UiSelector().text("雪盈福利").instance(0)'
    ')'
)

def test_user_login(self):
    self.driver.find_element_by_android_uiautomator(
        'new UiSelector().text("我的")'
    ).click()
    self.driver.find_element_by_android_uiautomator(
        'new UiSelector().text("登录雪球")'
    ).click()

def test_scroll_to_target(self):
    self.driver.find_element_by_android_uiautomator(
        'new UiSelector().text("关注")'
    ).click()
    self.driver.find_element_by_android_uiautomator(scroll_command).click()

  1. Scroll-Based Element Location

Dynamic content loading requires scroll-aware strategies. UiAutomator's UiScrollable class provides reliable solutions for:

  • Vertical/horizontal list navigation
  • Lazy-loaded content discovery
  • Off-screen element interaction

Combine with explicit waits to handle asynchronous rendering during automation flows.

Related Articles

Efficient Usage of HTTP Client in IntelliJ IDEA

IntelliJ IDEA incorporates a versatile HTTP client tool, enabling developres to interact with RESTful services and APIs effectively with in the editor. This functionality streamlines workflows, replac...

Installing CocoaPods on macOS Catalina (10.15) Using a User-Managed Ruby

System Ruby on macOS 10.15 frequently fails to build native gems required by CocoaPods (for example, ffi), leading to errors like: ERROR: Failed to build gem native extension checking for ffi.h... no...

Resolve PhpStorm "Interpreter is not specified or invalid" on WAMP (Windows)

Symptom PhpStorm displays: "Interpreter is not specified or invalid. Press ‘Fix’ to edit your project configuration." This occurs when the IDE cannot locate a valid PHP CLI executable or when the debu...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.