# 创建 ActionChains 对象 actions = ActionChainsdriver # 将鼠标移动到元素的某个位置 xy= calculate_new_positionxyelementsizewidthelementsizeheight actionsmove_to_element_with_offsetelement x
要将提供的x和y转换为相对于元素中心的坐标,可以根据元素的位置和大小计算出元素中心的坐标,并将x和y调整为相对于元素中心的坐标。
修改后的代码如下:
# 创建 ActionChains 对象
actions = ActionChains(driver)
# 计算元素中心的坐标
element_center_x = element.location["x"] + element.size["width"] / 2
element_center_y = element.location["y"] + element.size["height"] / 2
# 将提供的x和y转换为相对于元素中心的坐标
new_x = x - element_center_x
new_y = y - element_center_y
# 将鼠标移动到元素的中心位置
actions.move_to_element_with_offset(element, new_x, new_y)
# 在指定位置执行单击操作
actions.click(element).perform()
# 执行所有操作
actions.release().perform()
请注意,这里假设提供的x和y是相对于整个页面的坐标,而不是相对于浏览器窗口的坐标。如果x和y是相对于浏览器窗口的坐标,还需要考虑浏览器窗口的位置和大小来调整坐标
原文地址: http://www.cveoy.top/t/topic/iUZp 著作权归作者所有。请勿转载和采集!