鼠标的操作方法都封装在ActionChains类中,执行鼠标操作都需要调用perform()方法
1.鼠标的右击(context_click),示例如下:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://www.baidu.com")
right_click = driver.find_element_by_partial_link_text("新")
ActionChains(driver).context_click(right_click).perform()
2.鼠标悬停操作(move_to_element),示例如下:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
stop_element = driver.find_element_by_xpath("//a[@href='http://www.baidu.com/gaoji/preferences.html']")
ActionChains(driver).move_to_element(stop_element).perform()
3.鼠标双击操作(double_click),示例如下:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://www.baidu.com")
double_click = driver.find_element_by_xpath("//input[@id='kw']")
ActionChains(driver).double_click(double_click).perform()
4.鼠标的拖拽操作(drag_and_drop),示例如下:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://www.baidu.com")
#拖拽的元素
element = driver.find_element_by_xpath("//a[@name='']")
#拖拽到的目标元素
target = driver.find_element_by_xpath("//a[@name='']")
ActionChains(driver).drag_and_drop(element,target).perform()