Absolute Xpath

It uses a complete XML root path of HTML structure 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.

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

Example :

In the below example we are launching the app on an Android device and clicking on the button using id and enter the text using absolute XPath.

  • Launch the App on an Android device.
  • Click on the button using id.
  • Enter the text using Absolute XPath
  • Wait for 2 seconds
  • Close the App

Absolute_XPath.py

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time

# Step 1 : Create "Desired Capabilities"
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['automationName'] = 'UiAutomator2'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel3XL'
desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk')
desired_caps['appPackage'] = 'com.skill2lead.appiumdemo'
desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity'

# Step 2 : Create "Driver object"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

ele_id = driver.find_element(AppiumBy.ID,"com.skill2lead.appiumdemo:id/EnterValue")
ele_id.click()

# Step 3 : "Click on the button using XPath value"
ele_xapth = driver.find_element(AppiumBy.XPATH,"/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")
ele_xapth.click()
ele_xapth.send_keys("skill2lead")

# Step 4 : Wait for 2 seconds
time.sleep(2)

# Step 5 : Close the driver object
driver.quit()