要单击屏幕上的特定位置,我使用以下代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.set_window_size(500, 500)
driver.get('https://clickclickclick.click/')
actions = ActionChains(driver)
x_coord, y_coord = 250, 182 #coordinates of the button
actions.move_to_location(x_coord, y_coord).click().perform()
如果代码在Windows 10/11上运行,则这段代码单击网站上突出位置的按钮而不会引发错误。如果代码在Linux Mint上运行,则会发生以下错误:
AttributeError: 'ActionChains' object has no attribute 'move_to_location'
我在selenium的文档中没有找到这个函数。现在有没有人可以让move_to_location
也在Linux Mint上工作?提前感谢您的时间和精力!
编辑:为了进一步说明这一点,print(hasattr(actions, 'move_to_location'))
在Windows 10/11上返回True
,在Linux Mint上返回False
2条答案
按热度按时间zqdjd7g91#
ActionsChains
类没有名为move_to_location
的方法。但是,
ActionsBuilder
类有这个方法。见下面的代码:更新:从下面的链接检查官方文档中的
ActionChains
,你不会找到该方法。我建议重新检查你的代码,以确保move_to_location
被哪个类访问。它必须是ActionBuilder
https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html
txu3uszq2#
您可以使用
move_by_offset
而不是move_to_location