通过使用webdriver的Python API 中的ActionChains类实现的,当调用ActionChains类时,鼠标操作不会立即执行,而是将所有操作都存放在一个队列里,当调用perform()方法时,队列里的操作会依次执行
基本方法 | 简要概述 |
click(element) | 单独鼠标当前所在位置的元素 |
click_and_hold(element) | 按住鼠标的左键单独鼠标当前位置的元素 |
doule_click(element) | 双击鼠标当前位置所在的元素 |
drag_and_drop(source, target) | 鼠标拖动 Source:鼠标拖动的元素 Target:鼠标释放的目标元素 |
move_to_element(element) | 鼠标悬停到当前元素所在的位置 |
context_click(element) | 鼠标右击当亲位置元素 |
move_to | 移动 |
示例:
# coding = utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() driver.get("http://www.baidu.com") driver.implicitly_wait(3) # 定位到元素的位置 ele = driver.find_element_by_id(‘su‘) # 元素要移动到的目标位置 ele2 = driver.find_element_by_link_text(‘新闻‘) # 将鼠标移动到元素所在的位置 ActionChains(driver).move_to_element(ele).perform() # 执行元素的拖放操作 ActionChains(driver).drag_and_drop(ele, ele2).perform()
原文:https://www.cnblogs.com/museniuniu/p/13547791.html