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"]
Example :
In the below example we are launching the app on an Android device and clicking on the button using Relative XPath value.Relative_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'] = 'UiAutomator1' 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) # Step 3 : "Click on the button using XPath value" ele_xapth = driver.find_element(AppiumBy.XPATH,'//android.widget.Button[@resource-id="com.skill2lead.appiumdemo:id/EnterValue"]') ele_xapth.click() # Step 4 : Wait for 2 seconds time.sleep(2) # Step 5 : Close the driver object driver.quit()
Now let us see other types of locators in Relative Xpath.
Relative_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'] = ('/Users/sujithreddy/Documents/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) # Step 3 : "Click on the button using XPath value" #ele_xapth = driver.find_element(AppiumBy.XPATH,'//android.widget.Button[@content-desc="Btn1"]') - Using Xpath_and Content-Des #ele_xapth = driver.find_element(AppiumBy.XPATH,'//android.widget.Button[@resource-id="com.skill2lead.appiumdemo:id/EnterValue"]') - Using Xpath_and id #ele_xapth = driver.find_element(AppiumBy.XPATH,'//android.widget.Button[2]') - Using Xpath_and index #ele_xapth = driver.find_element(AppiumBy.XPATH,'//android.widget.Button[@text="ScrollView"]') - Using Xpath_and text ele_xapth = driver.find_element(AppiumBy.XPATH,'//android.widget.Button[@resource-id="com.skill2lead.appiumdemo:id/EnterValue"]') ele_xapth.click() # Step 4 : Wait for 2 seconds time.sleep(2) # Step 5 : Close the driver object driver.quit()