我正在尝试创建一个机器人,该机器人必须单击一些无法识别鼠标单击但可以识别触摸的元素,我在网络上进行了一些搜索,找到了一种模拟触摸事件的方法。 我写了这个
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)
不会引发错误,但是它不起作用,屏幕上没有任何变化。根据文档 https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/touch/TouchActions.html
tap
方法模拟mouse clicks
而不模拟touchscreen events
,所以我想知道是否有一种方法可以模拟touchscreen events
上的selenium
,或者这是正确的方法,我做错了。
我也尝试通过写touchactions.tap(element).perform()
而不是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()
但是它引发了这个错误
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
不知道为什么。
我正在使用gekodriver
,python 3
和windows 10
答案 0 :(得分:0)
有趣的问题!在这种情况下,点击可能不足,因此我们需要在chrome选项旁边使用double_tab :)
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)