Selenium自动化测试 —— 模拟鼠标键盘的操作事件

鼠标操作事件

在实际的web产品测试中,对于鼠标的操作,不单单只有click(),有时候还要用到右击、双击、拖动等操作,这些操作包含在ActionChains类中。

ActionChains类中鼠标操作常用方法:

首先导入ActionChains类: from selenium.webdriver.common.action_chains import ActionChains

context_click():右击

double_click():双击

drag_and_drop():拖动

move_to_element():鼠标移动到一个元素上

click_and_hold():按下鼠标左键在一个元素上(长按)

常用的链条命令

pause():停留、click():点击、release():释放、perform():执行

ActionChains(driver).move_to_element(元素对象).pause(秒).click(元素对象).release(元素对象).perform()

代码如下:

python 复制代码
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
 
current_path = os.path.dirname(os.path.abspath(__file__))  # 当前路径
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver路径
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie等
 
driver.get('https://www.baidu.com/')  # 打开网站
 
# 右击操作 context_click()
element_obj = driver.find_element(By.XPATH,'//input[@id="su"]')  # 右击百度一下
mouse_obj = ActionChains(driver)
mouse_obj.context_click(element_obj).perform()  # perform执行操作
 
# 点击操作 click()
element_obj = driver.find_element(By.XPATH,'//a[text()="hao123"]')  
mouse_obj = ActionChains(driver)
mouse_obj.click(element_obj).release(element_obj).perform()  # 点击hao123
 
# 长按操作 click_and_hold()
element_obj = driver.find_element(By.XPATH,'//a[text()="hao123"]')  
mouse_obj = ActionChains(driver)
mouse_obj.click_and_hold(element_obj).pause(10).release(element_obj).perform()  #长按 hao123 10秒后松开
 
# 鼠标移动到一个元素  move_to_element()
e1 = driver.find_element(By.XPATH,'//a[@name="tj_briicon"]')  
e2 = driver.find_element(By.XPATH,'//a[@name="tj_zhidao"]') 
mouse_obj = ActionChains(driver)
mouse_obj.move_to_element(e1).pause(3).click(e2).release(e2).perform()  # 链条命令  移动到 更多 元素上停顿3秒,然后点击 知道 元素

键盘操作事件

在实际的web测试工作中,需要配合键盘按键来操作,webdriver的keys()类提供键盘上所有按键的操作,还可以模拟组合键Ctrl_a,Ctrl+c/v等。

**前置条件:**导入Keys类

from selenium.webdriver.common.keys import Keys

页面上的键盘操作(从搜索框中按两下tab键)

driver.find_element(By.XPATH,'//input[@id="kw"]').click()

ActionChains(driver).send_keys(Keys.TAB).pause(1).send_keys(Keys.TAB).perform()

组合键操作ctrl+a、ctrl+c、ctrl+v

driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')

ActionChains(driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

备注:

1、在使用修饰键的时候需要key_down()和key_up()方法

修饰键包含ctrl、alt、shift

2、类似alt+F4 ctrl+alt+delete不能使用,这里的组合键只针对网页生效的

代码如下:

python 复制代码
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
 
current_path = os.path.dirname(os.path.abspath(__file__))  # 当前路径
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver路径
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie等
 
driver.get('https://www.baidu.com/')  # 打开网站
 
# 页面上的键盘操作   从搜索框中按两下tab键
driver.find_element(By.XPATH,'//input[@id="kw"]').click()
ActionChains(driver).send_keys(Keys.TAB).pause(1).send_keys(Keys.TAB).perform()
 
# 组合键操作 ctrl+a、ctrl+c、ctrl+v、shift+a
# 修饰键 ctrl、alt、shift
# ctrl+a ==> 按下ctrl、按下a、松开a、松开ctrl
driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
ActionChains(driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
 
# 备注:系统级别的组合键不能使用,因为这里的组合键都是只针对网页生效的
 

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
秃了也弱了。4 小时前
WireShark:非常好用的网络抓包工具
网络·测试工具·wireshark
敲上瘾6 小时前
Linux系统cgroups资源精细化控制基础
linux·测试工具·docker·压力测试·cgroups
zru_96028 小时前
Spring Boot 单元测试:@SpyBean 使用教程
spring boot·单元测试·log4j
郝学胜-神的一滴8 小时前
基于C++的词法分析器:使用正则表达式的实现
开发语言·c++·程序人生·正则表达式·stl
程序员曦曦10 小时前
10:00开始面试,10:06就出来了,问的问题有点变态。。。
自动化测试·软件测试·功能测试·程序人生·面试·职场和发展
gc_229911 小时前
使用HtmlAgilityPack+PuppeteerSharp+iText7抓取Selenium帮助文档
selenium·itext7·htmlagilitypack·puppeteersharp
天才测试猿12 小时前
常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·压力测试
OBOO鸥柏商用液晶显示厂家13 小时前
OBOO鸥柏丨75寸/86平板企业办公会议触控一体机核心国产化品牌招投标参数
计算机外设·电脑·大屏端·信息发布系统·会议一体机
程序员二黑15 小时前
单元测试三大神器:unittest vs JUnit vs Jest 终极对决
单元测试·测试·ab测试
python-行者17 小时前
akamai鼠标轨迹
爬虫·python·计算机外设·akamai