selenium如何将鼠标移动调使用绝对坐标 每次移动完后都要还原回去00
在Selenium中,可以使用ActionChains类来模拟鼠标操作。要将鼠标移动到绝对坐标上,可以使用move_by_offset方法,指定相对于当前位置的偏移量来移动鼠标。然后,使用move_to_element方法将鼠标移动到绝对坐标上。
以下是一个示例代码,展示了如何将鼠标移动到绝对坐标上,并在每次移动后都将鼠标还原回初始位置(0,0)。
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
# 创建 WebDriver 实例
driver = webdriver.Chrome()
# 打开网页
driver.get("http://example.com")
# 创建 ActionChains 实例
actions = ActionChains(driver)
# 获取初始位置
initial_position = driver.get_window_position()
# 将鼠标移动到绝对坐标 (100, 100)
actions.move_by_offset(100, 100).perform()
# 将鼠标移动到绝对坐标 (200, 200)
actions.move_by_offset(100, 100).perform()
# 将鼠标移动回初始位置 (0, 0)
actions.move_to_element_with_offset(driver.find_element_by_tag_name("html"), initial_position["x"], initial_position["y"]).perform()
# 关闭浏览器
driver.quit()
在上述示例代码中,我们使用get_window_position方法获取了初始位置,并在每次移动后使用move_to_element_with_offset方法将鼠标移动回初始位置
原文地址: https://www.cveoy.top/t/topic/iVcS 著作权归作者所有。请勿转载和采集!