Tap Gesture

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()


Here, tap() method takes four arguments.

  • To tap on an element, if we pass as “None” then it will tap given x , y coordinates. If we pass an element variable then it will tap on that particular element.
  • X - coordinate value
  • Y - coordinate value
  • Count : number of times to tap on an element.

Example: In the below example we are launching the app on an Android device and tap on the button using x , y coordinate values.

  • Launch the App on an Android device.
  • Create TouchAction class object
  • Using the TouchAction object, call the tap() method and pass all the arguments.
  • Wait for 2 seconds
  • Close the App

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.

  • Launch the App on an Android device.
  • Create TouchAction class object
  • Using the TouchAction object, call the tap() method and pass all the arguments along with the element variable.
  • Wait for 2 seconds
  • Close the App

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()