相信很多小伙伴刚开始学自动化的时候,对offset偏移量很迷,下面是我个人的理解,虽然不官方,但是好理解
```python
# Offset from Element (Top Left Origin) 从元素偏移(左上原点)
# 例子Move mouse here
# Selenium 4.3 已废弃,不纠结
def test_move_by_offset_from_element(driver):
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
sleep(3)
# 当元素不完全位于视窗内时,此方法无法正常工作,窗口打开时如果只展示一部分,会报错,需要
# 向下拖动窗口,先手工拖
mouse_tracker = driver.find_element(By.ID ,"mouse-tracker")
sleep(3)
#偏移量:以元素中间为原点的坐标系,正数表示向x轴/y轴正方向偏移(x偏移量要+1),负数反之
# 此方法移动到元素的视图中心点,然后将鼠标移动到提供的偏移量
ActionChains(driver)\
.move_to_element_with_offset(mouse_tracker, -10, -1)\
.perform()
sleep(3)
coordinates = driver.find_element(By.ID, "relative-location").text.split(", ")
# print(coordinates)
# n = abs(int(coordinates[0]) - 100 - 8)
# print(n)
# 绝对值,算偏差是否在2以内
# assert abs(int(coordinates[0]) - 100 - 8) < 2
# Offset from Viewport 从视窗偏移 此方法将鼠标从当前视口的左上角移动提供的偏移量。
#如果鼠标往下滑动,则以滑动后的页面左上角为起始点
# 例子Absolute Location
def test_move_by_offset_from_viewport_origin_ab(driver):
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
sleep(3)
mouse_tracker = driver.find_element(By.ID , "mouse-tracker")
sleep(3)
action = ActionBuilder(driver)
action.pointer_action.move_to_location(8,0)
action.perform()
sleep(3)