Waldo sessions now support scripting! – Learn more
Testing

Testing With Appium: How to Use XPath Locators

Ifeanyi Benedict Iheagwara
Ifeanyi Benedict Iheagwara
Testing With Appium: How to Use XPath Locators
January 17, 2023
8
min read

Appium is a tool for testing native, mobile web, and hybrid applications. It lets you locate elements on a page using XPath, a language for navigating elements and attributes in an XML document like an HTML page. 

With Appium, you can use XPath to find elements on the screen based on attributes like text, ID, or class.  

In this post, we'll see how to use Appium and XPath to locate and interact with elements on a page. Then, we'll discuss different types of XPath and how to handle dynamic elements using XPath in Appium. We'll also provide examples of using Appium with XPath in Python, Ruby, and Java.

By the end, you'll know how to use Appium and XPath together to test your applications. 

Appium and XPath for Testing

There are various mobile testing frameworks available for performing automated mobile testing. One is Appium, an open-source mobile app automation framework that tests all mobile applications written using Kotlin, Java, Python, Objective-C, Swift, React Native, and Flutter

XPath, on the other hand, is a Selenium technique to navigate through a page's HTML structure. This technique also helps find elements on a webpage using XML path expression.

Let's see how we can use Appium and XPath. 

How to Use Appium for XPath

Here's a step-by-step approach: 

  1. Start the Appium Inspector tool. This will allow you to inspect the elements of your app and find their XPath.
  2. Select the emulator or device you want to inspect. The Appium Inspector will show you the elements of the app running on the emulator or device.
  3. Inspect the element you want to locate using XPath. You can do this by clicking on the element in the Appium Inspector window. This will show you the element's properties, including its XPath.
  4. Copy the XPath of the element you want to locate. You'll need to use this XPath in your Appium script to interact with the element.
  5. Write your Appium script using the XPath of the element you want to interact with. You can use the find_element_by_xpath method to locate the element in your script. For example, if you want to locate an element with the XPath //android.widget.Button[@text='Sign In'], you can use the following code in your Appium script: 

button = driver.find_element_by_xpath("//android.widget.Button[@text='Sign In']")

Testing With Appium Using XPath Parameters in Multiple Languages

Now that we've covered the fundamentals of using Appium and XPath to locate and interact with elements on a page, let's dive into more specific examples.  

You can use Appium with XPath in different programming languages. Let's explore Python, Ruby, and Java. 

Python

To locate an element using XPath in Python with Appium, you can use the find_element_by_xpath method: 


element = driver.find_element_by_xpath("//button[@id='submit-button']")

You can also use the find_elements_by_xpath method to locate multiple elements that match the XPath:


elements = driver.find_elements_by_xpath("//a[contains(@href, 'example.com')]")

Ruby

To locate an element using XPath in Ruby with Appium, you can use the find_element(:xpath, "xpath_expression") method: 


element = driver.find_element(:xpath, "//button[@id='submit-button']")

You can also use the find_element(:xpath, "xpath_expression") method to locate multiple elements that match the XPath: 


elements = driver.find_elements(:xpath, "//a[contains(@href, 'example.com')]")

Java

To locate an element using XPath in Java with Appium, you can use the findElement (By.xpath("xpath_expression")) method: 


WebElement element = driver.findElement(By.xpath("//button[@id='submit-button']"));

You can also use the findElement (By.xpath("xpath_expression")) method to locate multiple elements that match the XPath: 


List<WebElement> elements = driver.findElements(By.xpath("//a[contains(@href, 'example.com')]"));

Using Different Types of XPath in Appium

Let's look at how to use different types of XPath in Appium to locate elements on a page. We'll use the fictional mobile application "My App" as an example. 

  • XPath using ID: To locate an element by its ID attribute, you can use an XPath expression like the following:

driver.find_element_by_xpath("//*[@id='submit-button']")

The first element on the page containing the ID property "submit-button" will be found using this XPath expression. 

  • XPath using Resource-ID: To locate an element by its resource-id attribute, you can use an XPath expression like the following:

driver.find_element_by_xpath("//*[@resource-id='com.example.myapp:id/menu_button']")

This XPath expression will locate the first element on the page with the resource-id attribute com.example.myapp:id/menu_button

  • XPath using Desc: To locate an element by its desc attribute, you can use an XPath expression like the following:

driver.find_element_by_xpath("//*[@desc='Search']")

The first element on the page containing the desc attribute "Search" will be found using this XPath expression. 

  • XPath using Text: To locate an element by its text attribute, you can use an XPath expression like the following:

driver.find_element_by_xpath("//*[@text='Sign In']")

This XPath expression will locate the first element on the page with the text attribute "Sign In." 

  • XPath using Contains: To locate elements that contain a specific value in one of their attributes, you can use the contains() function in your XPath expression. For example:

driver.find_elements_by_xpath("//*[contains(@class, 'menu-item')]")

The XPath expression above will locate all elements on the page with a class attribute containing the value "menu-item." 

  • XPath using Boolean operations: You can use Boolean operators like "and," "or," and "not" in your XPath expressions to specify more complex criteria for locating elements. For example:

driver.find_element_by_xpath("//*[@id='submit-button' and @type='button']")

This XPath expression will locate the first element on the page with an ID attribute of "submit-button" and a "button" type attribute. 

Now that we've examined the different types of XPath available in Appium, let's delve into how to handle dynamic elements in Appium using XPath. 

How to Handle Dynamic Elements in Appium Using XPath

To handle dynamic elements in Appium using XPath, you can use the following steps: 

  1. Identify the unique and stable attributes of the dynamic element, such as its class, ID, or name.
  2. Use the "contains" function in the XPath to match the unique attribute of the element. For example, if the element has a class name that contains the word btn, you can use the following XPath: 

//*[contains(@class,'btn')]
  1. Use the "index" function to locate the specific element within the set of elements that match the XPath. For example, if you want to locate the second element that contains the word btn in its class name, you can use the following XPath: 

//*[contains(@class,'btn')][2]
  1. Use the findElement or findElements method in Appium to locate the dynamic element using the XPath. For example: 

WebElement dynamicElement = driver.findElement(By.xpath("//*[contains(@class,'btn')][2]"));
  1. Once the dynamic element is located, you can interact with it using the standard Appium methods, such as "click" or "sendKeys."

It's essential to ensure that the unique attribute used in the XPath is stable and doesn't change between test runs. This will ensure that the XPath is always valid and can reliably locate the dynamic element. 

We've learned how to handle dynamic elements in Appium using XPath. Now, let's look at some troubleshooting tips to help you work through any issues you may encounter while using Appium. 

Troubleshooting Tips for Working With Appium

  • Verify that the correct XPath syntax is being used. This includes ensuring that the path accurately reflects the desired element's location in the app's hierarchy and that any attribute values are properly enclosed in quotation marks.
  • Check that the app has fully loaded before locating the desired element. This can be done using an Appium wait function, such as driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) to wait for the app to load fully before executing the XPath search.
  • If the desired element is not located, try using the Appium Inspector or uiautomatorviewer to view the app's hierarchy and verify that the XPath accurately reflects the element's location.
  • If the desired element is still not located, try using a different XPath search method, such as using the element's ID or class instead of its full hierarchy.
  • If all else fails, try using a different locator strategy, such as using the element's ID, class name, or a different automation framework.

Additional Information

How to Get XPath Text from Appium

To get the text of an element using Appium and XPath, you can use the get_attribute method and pass in the "text" attribute as a parameter. For example: 


# Locate the element using XPath
element = driver.find_element_by_xpath("//*[@text='Sign In']")

# Get the text of the element
text = element.get_attribute("text")
print(text)  # Output: "Sign In"

You can also use the text property of the element directly to get its text. For example: 


# Locate the element using XPath
element = driver.find_element_by_xpath("//*[@text='Sign In']")

# Get the text of the element
text = element.text
print(text)  # Output: "Sign In"

Remember that this will only work if the element has a visible text value. The get_attribute or text property will return an empty string if the element has no visible text. 

How to Locate a DOM Element and Find XPath in Appium

To locate a DOM element and find its XPath in Appium, you can use the inspector tool built into Appium or a tool like the Chrome developer tools. 

Here are the steps to locate an element and find its XPath using the Appium Inspector: 

  1. Start the Appium server and launch the app you want to test.
  2. In the Appium server console, click the "Launch" button next to the "Inspector" option. This will open the Appium Inspector tool in a new window.
  3. Use the inspector tool to navigate the page containing the element you want to locate.
  4. Click on the element in the inspector tool to highlight it in the app.
  5. The XPath for the element will be displayed in the "XPath" field at the top of the inspector window.

Conclusion

Appium is a powerful tool for testing native, mobile web, and hybrid applications. Its ability to locate elements on a page using XPath allows you to interact easily with and test these elements.

Using Appium and XPath together will enable you to write reliable and practical test scripts for your applications. 

If you're interested in using Appium for your testing needs, check out Waldo for more information on how our solution can help you streamline your testing process.

Automated E2E tests for your mobile app

Waldo provides the best-in-class runtime for all your mobile testing needs.
Get true E2E testing in minutes, not months.

Reproduce, capture, and share bugs fast!

Waldo Sessions helps mobile teams reproduce bugs, while compiling detailed bug reports in real time.