Exploring XPaths in Appium

Baala Murugan
3 min readAug 22, 2020

--

This article is completely based on my experience narrowed down with Appium, How I tried to overcome challenges while defining an efficient XPath.

Usually, we ask UI developers to add an ID tag to each element for the automation which becomes quite challenging for them sometimes and leads to back and forth changes in the code. So, let’s see how can we handle such cases which don’t have any unique element present.

  1. First, let's have a straight forward approach, say a happy flow. We have an ID included with the element which needs to be located. This makes our task much simpler.
Example with ID Attribute
//*[@id='user-details'] 

2. Now, is the case where we couldn’t find an ID associated with the element. Don't worry lookout for a text associated with it.

Example with Text Attribute
//*[contains(@text, 'TOTAL BALANCE')]

3. Using resource-id is another attribute present in many application which has a unique id with which the elements can be located .

Example with Resource Id Attribute
//*[@resource-id = 'com.dunzo.user:id/locatiom_icon']

4. Finding an element with a partial text identifier. This could be used in places where ID’s keeps on updating with a text in common.
For example: id_12345 , id_24221, id_43212

//*[contains(@resource-id,'id_')]

5. Using following-sibling method. If we want to extract the value of the amount loaded against the text i.e. ‘Corporate Load’ in below code snippet -

Example for following-sibling
UI Example for usage of following-sibling
//*[contains(@text, 'Corporate Load')]/following-sibling::android.view.View")

6. Using parent method. The parent element is basically used when you select an element and need to get the parent element.

Syntax — //*[XPath Expression]/child::className

//*[@resource-id='user-name']/parent::android.widget.EditText

These are a few ways that could be used to find the XPath in an efficient way. Instead of having long XPaths which might vary from device to device or structure changes. These techniques would be reliable across devices when tests are run across different devices.

--

--

Baala Murugan
Baala Murugan

Written by Baala Murugan

SDET @ Razorpay. Trying to make an impact in whatever I do.

No responses yet