We have TouchAction class to perform long press action on an element.
First we need to create an object for the TouchAction class and pass the driver object to it.
Now by using the TouchAction class object we can access the long press method and pass Mobile Element object along with duration of pressing an element in seconds.
Finally we need to call perform() method by using TouchAction class object,This is used to perform the action.
Syntax :
actions = TouchAction(driver) actions.long_press(ele, 5) actions.perform()
Example: In the below example we are launching the app on an Android device and long pressing on the button using a text locator by scrolling.
LongPressEx.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 : Create a MobileElement variable and store the long click button ele = wait.until(lambda x: x.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiScrollable(new UiSelector()).scrollIntoView(text("LONG CLICK"))')) # Step 5 : Create TouchAction class object actions = TouchAction(driver) # Step 6 : Call the long press method and pass the variable name on which we need to long press # along with the duration of the press actions.long_press(ele, 5) actions.perform() time.sleep(2) driver.quit()