windows (python Selenium)AttributeError:“ActionChains”对象没有属性“move_to_location”

yqhsw0fo  于 2023-04-22  发布在  Windows
关注(0)|答案(2)|浏览(206)

要单击屏幕上的特定位置,我使用以下代码:

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

zqdjd7g9

zqdjd7g91#

ActionsChains类没有名为move_to_location的方法。
但是,ActionsBuilder类有这个方法。见下面的代码:

actions = ActionBuilder(driver)
actions.pointer_action.move_to_location(<x_coord>, <y_coord>).click()
actions.perform()

更新:从下面的链接检查官方文档中的ActionChains,你不会找到该方法。我建议重新检查你的代码,以确保move_to_location被哪个类访问。它必须是ActionBuilder
https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html

txu3uszq

txu3uszq2#

您可以使用move_by_offset而不是move_to_location

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

driver = webdriver.Chrome()

driver.set_window_size(500, 500)
driver.get('https://clickclickclick.click/')

actions = ActionChains(driver)

x_coord, y_coord = 250, 182 #coordinates of the button
t = actions.move_by_offset(x_coord, y_coord).click().perform()
time.sleep(5)

相关问题