App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide XPath in Appium: Tutorial

XPath in Appium: Tutorial

By Mohammed Waseem, Community Contributor -

A web application, native or hybrid application, consists of various elements that make up the entire user interface for that particular application. During the testing, the automation tools make use of locators that helps in identifying a particular element in an application. These locators in Appium can range from a regular class name, accessibility ID, Xpath, etc. In this article, we will discuss how to use XPath in Appium to locate elements while writing test scripts.

What is XPath?

Before getting into the specifics, it is necessary to understand what exactly an XPath is and how it helps as a locator. 

XPath is nothing but XML Path or an expression language that is used to query or transform XML documents. The XPath consists of the following properties:

  1. One of the many ways to locate UI elements in web or mobile applications
  2. Xpath is a World Wide Web consortium recommendation. 
  3. Xpath has a path-like syntax that helps in navigating through the UI elements of an application. 

Even though Xpath is one of the locators for UI elements, it is not one of the conventional choices of many Quality Assurance professionals or Mobile/Web app testing professionals. Let’s examine some of the advantages and disadvantages of using Xpath in testing.  

Advantages and Limitations of using XPath

Advantages of using XpathLimitations of using Xpath
Built-in Functions – XPath comes with around 200 built-in functions for string values, boolean values, etc. If the UI elements of the application constantly change during development. The XPath makes the changes expensive with each build.
Xpath allows you to create selectors for complex problems where you can search for elements where only the text is available. Xpath is untidy and slow compared to other locators for UI elements in an application. 

How do you use Xpath in Appium?

In a conventional setting, you may have to go through the following steps – not necessarily in the exact order.

  1. Getting the Appium server started
  2. Creating the desired capabilities 
  3. Choosing the environment, OS, etc.
  4. Starting up the Emulator through the Android App
  5. Creating a test script in a different environment
  6. And making sure all the system dependencies and test dependencies are already installed and running. 

Note: Sometimes unforeseen circumstances or mere slight system dependencies can significantly hinder running a simple Appium script. 

To avoid this, you can simply use Browserstack App Automate by following the steps written below. 

Step 1 Download a sample app, or you can also upload your own application. Using the following Python script. 


import requests

files = {

    'file': open(r'C:\Users\waseem k\Downloads\WikipediaSample.apk', 'rb'),


response ='',


                         auth=("mohammadwaseem_r3b75X", "JxCJvHgZM5cxPrir6zHS"))



Uploading APK file to test on BrowserStack App Automate using Python

Step 2 The output will give you a URL for the application that you have uploaded. You will use that URL in the script given below to run a sample build using Appium and Xpath.

Step 3 Use the following Python Script to run the Appium test using Xpath.


from appium import webdriver

from appium.webdriver.common.mobileby import MobileBy

from import WebDriverWait

from import expected_conditions as EC

import time

desired_cap = {

    # Set your access credentials

    "browserstack.user": "",

    "browserstack.key": "",

    # Set URL of the application under test

    "app": "bs://c700ce60cf13ae8ed97705a55b8e022f13c5827c",

    # Specify device and os_version for testing

    "device": "Google Pixel 3",

    "os_version": "9.0",

    # Set other BrowserStack capabilities

    "project": "First Python project",

    "build": "browserstack-build-1",

    "name": "first_test"



Python Script to run appium test using Xpath

Note: You will need to install dependencies like Appium in Python and describe the desired capabilities as shown in the script.

Step 4 The sample test will install the application on the device and search for the keyword given in the script. And in the end, add Asserts for the results in the list.


# Initialize the remote Webdriver using BrowserStack remote URL

# and desired capabilities defined above

driver = webdriver.Remote(




# Test case for the BrowserStack sample Android app.

# If you have uploaded your app, update the test case here.

search_element = WebDriverWait(driver, 30).until(

    EC.element_to_be_clickable((MobileBy.XPATH, "(//*[(@content-desc='Search Wikipedia')])[1]"))


search_input = WebDriverWait(driver, 30).until(

    EC.element_to_be_clickable((MobileBy.XPATH, "(//*[(@text='Search Wikipedia')])[1]")




search_results = driver.find_element_by_xpath("//*[(@resource-id='org.wikipedia.alpha:id/page_list_item_container')])")

assert (len(search_results) > 0)

# Invoke driver.quit() after the test is done to indicate that the test is completed.



Step 5 You can display the results in the dashboard, as seen below.

Xpath in Appium Test ResultAs you can see, the logs show how the application was launched, and one of the elements was located using the XPath.


There is no hard and fast rule for using any specific selector to locate a UI element in a web or mobile application. So is the case with Xpath, be it in Appium or selenium. As a wise QA professional, you must always rely on project requirements and, based on those, create your testing strategies. Even though XPath in test scripts may not be a wise choice in the QA community, Appium saves the day.


Appium Mobile App Testing

Featured Articles

Quick XPath Locators Cheat Sheet

Effective Locator Strategies in Appium

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack