前言:我们在做web自动化的时候,有时候页面的元素不需要我们点击,值需要把鼠标移动上去就能展示各种信息,这个时候我们可以通过操作鼠标来实现,接下来我们来讲一下使用selenium做web自动化的时候如何来操作鼠标。

鼠标操作,我们可以使用selenium的ActionChains类来实现,我们先来认识一下这个类。

一、ActionChains类常用方法
click_and_hold(on_element=None) :点击鼠标左键,不松开
context_click(on_element=None):点击鼠标右键
double_click(on_element=None):双击鼠标左键
drag_and_drop(source, target):拖拽到某个元素然后松开
move_by_offset(xoffset, yoffset):鼠标从当前位置移动到某个坐标
move_to_element(to_element) :鼠标移动到某个元素
release(on_element=None):在元素上释放按住的鼠标按钮
pause(seconds):暂停操作(秒)
二、ActionChains类所有方法
perform(self)–执行鼠标操作方法
reset_actions()–清楚操作子令
click(on_element=None)–点击鼠标左键
click_and_hold(on_element=None):点击鼠标左键,不松开
context_click(on_element=None):点击鼠标右键
double_click(on_element=None):双击鼠标左键
drag_and_drop(source, target):拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) :拖拽到某个坐标然后松开
key_down(value, element=None):按下某个键盘上的键
10、key_up(value, element=None) :松开某个键
11、move_by_offset(xoffset, yoffset):鼠标从当前位置移动到某个坐标
12、move_to_element(to_element) :鼠标移动到某个元素
13、move_to_element_with_offset(to_element, xoffset, yoffset):移动到距某个元素(左上角坐标)多少距离的位置
14、pause(seconds):暂停操作(秒)
15、release(on_element=None):在元素上释放按住的鼠标按钮
16、send_keys(*keys_to_send):发送某个键到当前焦点的元素
17、send_keys_to_element(element, *keys_to_send) :发送某个键到指定元素
三、ActionChains使用步骤
实例化:actions = ActionChains(driver)
调用鼠标操作方法:actions.move_to_element(menu)
执行鼠标操作方法:actions.perform()
四、实战
代码做的事情:

打开测试网页:https://www.runoob.com/try/try.php?filename=tryjs_events_mouseover
切换iframe
定位到绑定鼠标事件的div
将鼠标移动到div上触发事件(可以观察到div上字会发生变化)

import time
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(r"D:\chromeDriver\71\chromedriver71.exe")
driver.implicitly_wait(5)
driver.get(url="https://www.runoob.com/try/try.php?filename=tryjs_events_mouseover")
driver.maximize_window()
#切换iframe
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@id="iframeResult"]'))
#元素定位
ele = driver.find_element_by_xpath('//div[@onmouseover="mOver(this)"]')
#移动鼠标到元素上触发事件
actions = ActionChains(driver)
actions.move_to_element(ele)
actions.perform()
time.sleep(5)
driver.quit()

六、总结
ActionChains类中其他方法使用方式都一样,大家有空可以试一下
ActionChains类中有很多方法,但是常用的并不多,上面已经列出,掌握常用方法即可
特别注意:ActionChains的执行原理,当你调用ActionChains的方法时,不会立即执行,而是会将所有的操作按顺序存放在一个队列里,当你调用perform()方法时,队列中的时间会依次执行。
ActionChains类中的方法可以使用链式调用,大家思考思考自己拓展吧(其实很简单)。

 

demo:

# selenium可以模拟鼠标的操作。
# 使用步骤:
# 1.创建ActionChains对象,导包
# 2.使用ActionChains对象的方法,进行操作
# 3.通过ActionChains"提交"这些操作


from selenium import webdriver
from time import sleep
# 需要导包
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver. common.by import By
from selenium.webdriver. common.keys import Keys

# webdrive 获取浏览器对象
driver = webdriver.Chrome("chromedriver.exe")  # 获取浏览器(Chrome)驱动
# 准备一个网址
url = "https://www.baidu.com/"
driver.get(url)  # 将地址发送给浏览器(不打开新页面)

# 创建action对象
action = ActionChains(driver)
sleep(1)
# context_click() 右击-->此方法模拟鼠标右键点击效果
action.context_click(driver.find_element(By.ID, "kw"))
# 事件的操作必须执行
#   perform()执行-->此方法用来执行以上所有鼠标方法
action.perform()
# double_click() 双击-->此方法模拟双标双击效果
# drag_and_drop() 拖动-->此方法模拟双标拖动效果
action.drag_and_drop(driver.find_element(By.LINK_TEXT, "hao123"), driver.find_element(By.ID, "kw"))# 将某个元素拖到某个元素然后放开
# move_to_element() 悬停-->此方法模拟鼠标悬停效果
action.move_to_element(driver.find_element(By.CLASS_NAME, "soutu-btn")).perform()
# 其他 可参见ActionChains的源码

sleep(5)  # 推迟执行5秒
# 回收资源
driver.quit()

 

来源:https://blog.csdn.net/weixin_54542209/article/details/123282059