To tap on any element first we need to find coordinates of (x and y axis) of a mobile element.
We need to turn on the pointer location in device settings to find the x and y coordinates of an element.
Go to settings > Tap build number 7 time > click on Developer options > turn on "Pointer location" toggle.
Now point your finger on the element which to tap and we can see the x and y coordinate values of an element on the top of the screen.
Syntax :
actions = TouchAction(driver) actions.tap(None,700,1990,1) actions.perform()
Example: In the below example we are launching the app on an Android device and tap on the button using x , y coordinate values.
TapGestureEx.py
from appium import webdriver from appium.webdriver.common.touch_action import TouchAction 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) # Step 3 : Create the object the TouchAction class actions = TouchAction(driver) # Step 4 : Call the tap() method and pass the arguments. actions.tap(None,700,1990,1) actions.perform() time.sleep(2) driver.quit()
Example: In the below example we are launching the app on an Android device and tap on an element at specified x , y coordinate values.
TapGestureEx2.py
from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException from selenium.webdriver.support.wait import WebDriverWait 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) # Step 3 : Create "WebDriver object" wait = WebDriverWait(driver, 25, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException]) # Step 4 : Using explicit wait create a variable for a button ele = wait.until(lambda x: x.find_element(AppiumBy.ID,"com.skill2lead.appiumdemo:id/EnterValue")) # Step 5 : Create the object the TouchAction class and call tap() method actions = TouchAction(driver) actions.tap(ele, 940, 2400, 1) actions.perform() time.sleep(2) driver.quit()