By XPath

What is XPath?

Identifying the element by using the XML path of the HTML structure to locate the element is known as XPath.


Identifying the MobileElement by “XPath”. It takes String as an argument. If an element isn't identified it throws an exception as No Such Element Exception.


Syntax :

driver.find_element(AppiumBy.XPATH,"Locator_Value")


We have two types of XPath

  • Absolute Xpath.
  • Relative Xpath

Absolute XPath :

It uses a complete XML root path to the required MobileElement.

This Absolute XPath is not recommended because whenever the XML structure changes then we need to update the XPath as well every time.

Absolute XPath

Syntax :

/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.EditText

'/'  - finding the element inside the parent element
'//' - finding the child or nested-child element inside the parent element

Relative Xpath :

It uses the direct path of a MobileElement using (id,className,attribute values , sub-string,etc) to perform action on it.

Syntax :

//android.widget.Button[@resource-id="com.skill2lead.appiumdemo:id/EnterValue"]

How to write the XPath

  • First we need keep “//” as we need to find the element inside the child element
  • Now we need to write the tag name, Here tag name is xml structure name or class name in android.
  • Now we need to define attribute name
  • Finally we need to pass the value

//tag[@attribute='value']


//android.widget.Button[@resource-id="com.skill2lead.appiumdemo:id/EnterValue"]