注意:必须创建一个事件链对象(ActionChains);最后鼠标事件链完成之后一定要进行事务提交perform()(如果事件链不提交那么所有的鼠标事件都不会执行)
1. 左键单击:click()
2. 在元素上按住鼠标左键,不松开:click_and_hold( on_element=None )
3. 右击:context_click()
4. 左键双击:double_click()
5. 点击鼠标左键,按住不放:click_and_hold()
6. 在某个元素位置松开鼠标左键:release(on_element=None)
7. 拖动某个元素至目标元素处:drag_and_drop(source, target)
8. 鼠标移动到某个元素:move_to_element(element)
9. 鼠标移动到距离当前位置(x,y)的地方:move_by_offset(xoffset, yoffset)
10.将鼠标移动到距某个元素多少距离的位置:move_to_element_with_offset(to_element, xoffset, yoffset)
11.执行所有 ActionChains 中存储的行为:perform()
12.在源元素上按住鼠标左键,然后移动到目标偏移并释放鼠标按钮:drag_and_drop_by_offset(源, xoffset , yoffset )
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import Keys, ActionChains
driver = webdriver.Chrome()
url = "https://www.baidu.com/"
driver.maximize_window ()
element_kw = driver.find_element(By.XPATH, '//*[@id="kw"]')
element_kw.send_keys('11112')
element_su = driver.find_element(By.XPATH,'//*[@id="su"]')
actions = ActionChains(driver)
actions.click_and_hold(element_su).release(element_su).perform()
actions.context_click(element_su).perform()
actions.double_click(element_su).perform()
url_drag = 'file:///D:/study/%E8%AF%BE%E5%A0%82%E8%B5%84%E6%96%99/14drag.html'
el4 = driver.find_element(By.XPATH, '//*[@id="div1"]')
el5 = driver.find_element(By.XPATH, '//*[@id="div2"]')
actions.drag_and_drop(el4,el5).perform()
element_set = driver.find_element(By.LINK_TEXT,"设置")
actions.move_to_element(element_set).perform()
actions.move_by_offset(0,100).perform()
element_set = driver.find_element(By.LINK_TEXT,"设置")
actions.move_to_element_with_offset(element_set,100,100).perform()
url_drag = 'file:///D:/study/%E8%AF%BE%E5%A0%82%E8%B5%84%E6%96%99/14drag.html'
el5 = driver.find_element(By.XPATH, '//*[@id="div2"]')
actions.click_and_hold(el5).perform()
actions.drag_and_drop_by_offset(el5,500, 500).perform()