1 源码路径
selenum/webdriver/common/action_chains.py
如果是对python自动化测试、web自动化、接口自动化、移动端自动化、面试经验交流等等感兴趣的测试人,可以 点这自行获取…
2 功能描述
提供鼠标操作,模拟用户的鼠标行为;
如鼠标的各种单击、双击、滑动、拖拽等操作。
3 使用方法
源码说的很清晰了,只要使用以下的方法即可:
ActionChains can be used in a chain pattern::
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
Or actions can be queued up one by one, then performed.::
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
4 几个方法
4.1 init方法
需要传递一个driver,这个在实际应用中就是使用这个类的时候需要传递一个driver
源码:
def __init__(self, driver):
"""
Creates a new ActionChains.
:Args:
- driver: The WebDriver instance which performs user actions.
"""
self._driver = driver
self._actions = []
if self._driver.w3c:
self.w3c_actions = ActionBuilder(driver)
4.2 perform方法
用来执行存储的所有动作或操作;
源码:
def perform(self):
"""
Performs all stored actions.
"""
if self._driver.w3c:
self.w3c_actions.perform()
else:
for action in self._actions:
action()
4.3 reset_actions方法
用来清空所有的动作;
源码:
def reset_actions(self):
"""
Clears actions that are already stored locally and on the remote end
"""
if self._driver.w3c:
self.w3c_actions.clear_actions()
self._actions = []
### 5 ActionChains类API
![image.png](https://upload-images.jianshu.io/upload_images/28060302-1e5dba9707d4180e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6 实例
输入百度网址
输入NoamaNelson
回车搜索
最大化窗口
全选输入的内容,重新输入N
点击“百度一下”进行搜索
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/5/16
# 文件名称:selen_stu.py
# 作用:打开百度输入NoamaNelson
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("NoamaNelson")
time.sleep(1)
driver.find_element_by_id("kw").send_keys(Keys.ENTER)
driver.maximize_window()
time.sleep(0.5)
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,"a")
time.sleep(0.5)
driver.find_element_by_id("kw").send_keys("N")
su = driver.find_element_by_id("su")
ActionChains(driver).click(su).perform()
time.sleep(1)
driver.quit()
最后:你如果是对python自动化测试、web自动化、接口自动化、移动端自动化、面试经验交流等等感兴趣的测试人,可以 点这自行获取…