导入必要的库

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

创建浏览器对象

driver = webdriver.Chrome()

打开网页

driver.get("https://www.example.com")

创建 ActionChains 对象

actions = ActionChains(driver)

移动鼠标到相对网页的0,0位置

actions.move_by_offset(0, 0)

继续移动鼠标到指定的坐标位置

actions.move_by_offset(x, y)

点击鼠标左键

actions.click()

执行操作

actions.perform() 为什么这样做点击的位置会还原,,内容:这是因为在执行完点击操作后,鼠标会返回到初始位置,即相对网页的0,0位置。如果需要在点击后保持鼠标位置不变,可以使用actions.click_and_hold()方法,这样鼠标会一直保持按下状态,直到执行actions.release()释放鼠标。示例代码如下:

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

driver = webdriver.Chrome()
driver.get("https://www.example.com")

actions = ActionChains(driver)

# 移动鼠标到指定位置
actions.move_by_offset(x, y)

# 按下鼠标左键
actions.click_and_hold()

# 执行操作
actions.perform()

# 释放鼠标左键
actions.release()
Selenium 自动化: 使用 ActionChains 保持鼠标位置

原文地址: https://www.cveoy.top/t/topic/qA50 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录