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:\Users\mcara\PycharmProjects\1\venv\lib\site-packages\selenium\webdriver\common\touch_actions.py", line 47, in perform
action()
File "C:\Users\mcara\PycharmProjects\1\venv\lib\site-packages\selenium\webdriver\common\touch_actions.py", line 57, in <lambda>
Command.SINGLE_TAP, {'element': on_element.id}))
File "C:\Users\mcara\PycharmProjects\1\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\mcara\PycharmProjects\1\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With