我试图创建一个机器人,这个机器人必须点击一些元素,不承认鼠标点击,但承认触摸,我在网上搜索了一下,我发现了一种方法来模拟触摸事件。
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.htmltap
方法模拟mouse clicks
,而不是touchscreen events
,所以我想知道是否有一种方法可以在selenium
上模拟touchscreen events
,或者这是正确的方法,但我做错了。
我也试过写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
2条答案
按热度按时间dluptydi1#
有趣的问题!也许点击在这种情况下是不够的,相反,我们需要使用double_tab旁边的chrome选项:)
xriantvc2#
您也可以尝试:
发现于:appium-python-客户端