Python selenium键盘鼠标事件实现过程详解
作者:安琪儿一直在 时间:2021-09-16 05:26:23
引言
----在实际的web测试工作中,需要配合键盘按键来操作,webdriver的 keys()类提供键盘上所有按键的操作,还可以模拟组合键Ctrl+a,Ctrl+v等。
举例:
#cording=gbk
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By #导入by方法
from selenium.webdriver.common.action_chains import ActionChains ##对鼠标事件操作
from selenium.webdriver.common.keys import Keys # 对键盘事件操作
current_path=os.path.dirname(__file__)
firefox_path=current_path+"/../webdriver/geckodriver.exe"
driver=webdriver.Firefox(executable_path=firefox_path)
driver.get("http://www.baidu.com")
# 先输入百度
driver.find_element_by_id('kw').send_keys('百度')
time.sleep(3)
# 1.删除度
driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)
time.sleep(3)
#2.清空输入框,重新输入值
driver.find_element_by_id('kw').clear()
driver.find_element_by_id('kw').send_keys('安琪儿')
time.sleep(5)
# 3.ctrl+a 全选输入框里的内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')
time.sleep(3)
# 4.ctrl+x 剪切输入框里的内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')
time.sleep(3)
# 5. ctrl+v 粘贴剪切的内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'v')
time.sleep(3)
# 6. 回车
driver.find_element_by_id('su').send_keys(Keys.ENTER)
time.sleep(3)
在实际的web产品测试中,对于鼠标的操作,不单单只有click(),有时候还要用到右击、双击、拖动等操作,这些操作包含在ActionChains类中。
ActionChains类中鼠标操作常用方法:
context_click() :右击
double_click() :双击
drag_and_drop() :拖动
move_to_element() :鼠标移动到一个元素上
举例:
#cording=gbk
import os
from selenium import webdriver
from selenium.webdriver.common.by import By #导入by方法
from selenium.webdriver.common.action_chains import ActionChains ##对鼠标事件操作
current_path=os.path.dirname(__file__)
firefox_path=current_path+"/../webdriver/geckodriver.exe"
driver=webdriver.Firefox(executable_path=firefox_path)
driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby9teS5odG1s.html")
mouse=ActionChains(driver) #创建一个鼠标对象
# element1=driver.find_element(By.XPATH,"//img[@src='/zentao/theme/default/images/main/zt-logo.png']") #Xpath利用属性定位
element1=driver.find_element(By.XPATH,"//img[contains(@src,'images/main/zt-logo.png')]") #xpath使用包含属性方法定位
mouse.context_click(element1).perform() #执行鼠标右击,.perform() 表示执行
element2=driver.find_element(By.XPATH,"//button[@type='button' and @class='btn' ]") #多属性定位
mouse.move_to_element(element2).perform() #移动到这个元素上
#对元素进行截图
driver.find_element(By.XPATH,"//button[@id='submit'][@type='submit']").screensh
来源:https://www.cnblogs.com/123anqier-blog/p/12729482.html
标签:Python,selenium,键盘,鼠标
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python+splinter自动刷新抢票功能
2023-07-08 11:33:42
Python实现学生管理系统的完整代码(面向对象)
2023-09-11 19:08:55
![](https://img.aspxhome.com/file/2023/4/100984_0s.png)
Python 京东云无线宝消息推送功能
2021-08-19 09:45:57
python logging.info在终端没输出的解决
2022-04-15 20:39:52
在python中使用with打开多个文件的方法
2023-05-03 16:33:45
python实战之Scrapy框架爬虫爬取微博热搜
2022-07-08 02:09:50
![](https://img.aspxhome.com/file/2023/2/89012_0s.png)
python数据类型强制转换实例详解
2022-02-08 05:22:43
asp如何自动反馈电子邮件?
2002-01-01 06:54:00
Django接收照片储存文件的实例代码
2022-06-01 09:05:29
PHP面向对象程序设计继承用法简单示例
2023-11-22 08:09:42
解决tensorflow训练时内存持续增加并占满的问题
2021-09-24 02:48:05
Python页面加载的等待方式总结
2023-07-24 23:42:30
浅谈python3中input输入的使用
2022-05-15 22:32:57
![](https://img.aspxhome.com/file/2023/6/81856_0s.png)
CSS 的优先规则
2009-01-08 12:40:00
Python实现查看系统启动项功能示例
2022-12-27 17:03:14
基于JS判断iframe是否加载成功的方法(多种浏览器)
2023-08-24 04:14:52
Python自动化测试之登录脚本的实现
2022-11-18 07:30:50
![](https://img.aspxhome.com/file/2023/8/81628_0s.jpg)
PHP echo()函数讲解
2023-06-05 18:50:54
![](https://img.aspxhome.com/file/2023/0/55390_0s.png)
ASP使用fso遍历文件及文件夹列出文件名
2008-10-27 19:32:00
Python实现常见数据格式转换的方法详解
2023-04-16 03:48:16
![](https://img.aspxhome.com/file/2023/0/115880_0s.png)