How to simulate touchscreen in selenium?

Question:

I’m trying to create a bot and this bot have to click some elements that doesn’t recognize the mouse click but recognize the touch, i searched a bit on the web and i found a way for simulate touch events.
I wrote this

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.touch_actions import TouchActions

user_agent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", user_agent)
driver = webdriver.Firefox(profile)
driver.set_window_size(400, 800)

WebDriverWait(driver, 10).until(lambda d: d.find_element_by_css_selector(".qJfNm").is_displayed())
element = driver.find_element_by_css_selector(".qJfNm")
touchactions = TouchActions(driver)
touchactions.tap(element)

and not error are raised but it doesn’t work, nothing change on the screen. According to the docs
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/touch/TouchActions.html

the tap methods simulates mouse clicks and not touchscreen events, so i was wondering if there is a way for simulate touchscreen events on selenium, or this is the correct way and i’m doing it wrong.

I tried too by writing touchactions.tap(element).perform() instead of touchactions.tap(element)

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.touch_actions import TouchActions

user_agent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", user_agent)
driver = webdriver.Firefox(profile)
driver.set_window_size(400, 800)

WebDriverWait(driver, 10).until(lambda d: d.find_element_by_css_selector(".qJfNm").is_displayed())
element = driver.find_element_by_css_selector(".qJfNm")
touchactions = TouchActions(driver)
touchactions.tap(element).perform()

but it raised this error

Traceback (most recent call last):
  File "C:/Users/mcara/OneDrive/Desktop/instagram bot mobile/instagram_bot_mobile.py", line 57, in <module>
    touchactions.tap(element).perform()
  File "C:UsersmcaraPycharmProjects1venvlibsite-packagesseleniumwebdrivercommontouch_actions.py", line 47, in perform
    action()
  File "C:UsersmcaraPycharmProjects1venvlibsite-packagesseleniumwebdrivercommontouch_actions.py", line 57, in <lambda>
    Command.SINGLE_TAP, {'element': on_element.id}))
  File "C:UsersmcaraPycharmProjects1venvlibsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:UsersmcaraPycharmProjects1venvlibsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: POST /session/71d75201-9012-46a1-9c6e-1c720dd332ce/touch/click did not match a known command

and don’t understand why.

I’m using gekodriver, python 3 and windows 10

Asked By: Mat.C

||

Answers:

Interesting question! Perhaps tap isn’t sufficient in this case and instead we need to use double_tab alongside chrome options 🙂

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

mobile_emulation = { "deviceName": "Nexus 6" }

chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

driver = webdriver.Chrome(chrome_options = chrome_options)
    WebDriverWait(driver, 10).until(lambda d: d.find_element_by_css_selector(".qJfNm").is_displayed())
    element = driver.find_element_by_css_selector(".qJfNm")
    touchactions = TouchActions(driver)
    touchactions.double_tab(element)
Answered By: David S

As TouchActions are deprecated, you can use:

from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder

actions = ActionChains(driver)

# override as 'touch' pointer action
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(2)
actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
actions.w3c_actions.pointer_action.release()
actions.perform()

found on: appium-python-client

Answered By: kaliiiiiiiii
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.