鼠标操作类:action_chains模块的ActionChains类
使用组成:操作 + 执行(perform())
导入代码
from selenium.webdriver.common.action_chains import ActionChains
查看代码常用操作有:
click(self, on_element=None)
click_and_hold(self, on_element=None) #Holds down the left mouse button on an element.
context_click(self, on_element=None) #Performs a context-click (right click) on an element.
double_click(self, on_element=None)
drag_and_drop(self, source, target) #Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
move_to_element(self, to_element) #Moving the mouse to the middle of an element.
release(self, on_element=None) #Releasing a held mouse button on an element.
perform() #调用这个函数执行鼠标操作
示例:
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com/")
sleep(1)
#找到元素
loc = (By.ID,"s-usersetting-top")
ele = driver.find_element(*loc)
#对元素进行鼠标操作
#实例化ActionChains类
ac = ActionChains(driver)
# 把鼠标悬浮在这个元素
ac.move_to_element(ele)
# ac.move_to_element(ele).click(ele) #鼠标悬浮并点击。可把两个动作拆开写
#执行操作。鼠标操作最后要有一个perform(),执行perform()后面还有鼠标操作代码时,需要再调用一次perform()
ac.perform()
sleep(3)
driver.quit()
原文:https://www.cnblogs.com/sue2015/p/14780740.html