selenium-Python之鼠标事件
通过click()来模拟鼠标的单击操作,鼠标还具有鼠标右击,双击,悬停甚至鼠标拖动等功能。在webdriver中,将这些鼠标操作方法封装在ActionChains类提供。
ActionChains类提供鼠标的常用方法:
1)perform():执行所有ActionChains中存储的行为
2)context_click():右击
3)double_click():双击
4)drag_and_drop():拖动
5)move_to_element(): 鼠标悬停
1.鼠标右击操作
对于ActionChains类所提供的的鼠标方法与click()方法的用法有所不同。
代码:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("http://192.168.xxx.xxxx:1080/")
#定位到要右击的元素
right_click = driver.find_element_by_xpath("/html/body/nav/ul/li[5]/a")
#对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(right_click).perform()
driver.quit()
注:
1.from selenium webdriver import ActionChains 导入提供鼠标操作的ActionChains类。
2.ActionChains(driver)调入ActionChains()类,将浏览器驱动driver作为参数传入。
3.contex_click()方法用于模拟鼠标右键操作,在调用时需要指定元素定位。
4.perform()执行所有ActionChains中存储的行为,可以理解为对整个操作的提交动作
2.鼠标悬停
鼠标悬停弹出下栏菜单是一个常见的功能设计
move_to_element()方法可以模拟鼠标悬停的动作,其用法与context_click()相同。
3.鼠标双击操作
double_click()方法用于模拟鼠标双击操作。
4.鼠标拖放操作
drag_and_drop(coure,target)在源元素上按住鼠标左键,然后移动到目标元素上释放。
1.soure()鼠标拖放的源元素。
2.target:鼠标释放的目标元素
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("http://192.168.10.61:1080/")
#定位到要右击的元素
right_click = driver.find_element_by_xpath("/html/body/nav/ul/li[5]/a")
#对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(right_click).perform()
above = driver.find_element_by_xpath("/html/body/nav/ul/li[1]/a")
#对定位的元素执行悬停操作
ActionChains(driver).move_to_element(above).perform()
#定位到要悬停的元素
double_click=driver.find_element_by_xpath("/html/body/nav/ul/li[2]/a")
#对定位的元素执行双击操作
ActionChains(driver).double_click(double_click).perform()
#定位元素的原位置
element = driver.find_element_by_xpath("/html/body/nav/ul/li[3]/a")
#定位元素要移动到的目标位置
target = driver.find_element_by_xpath("/html/body/nav/ul/li[4]/a")
#执行元素的拖放操作
ActionChains(driver).drag_and_drop(element,target).perform()
driver.quit()
selenium-Python之鼠标事件的更多相关文章
- Selenium WebDriver 中鼠标事件(全)
Selenium WebDriver 中鼠标事件(全) 鼠标点击操作 鼠标点击事件有以下几种类型: 清单 1. 鼠标左键点击 Actions action = new Actions(driv ...
- python+selenium三:鼠标事件与键盘事件
1.鼠标事件:# 每个模拟事件后需加.perform() 才会执行# context_click() 右击# double_click() 双击# drag_and_drop(source, targ ...
- python+selenium_鼠标事件
引言--在实际的web产品测试中,对于鼠标的操作,不单单只有click(),有时候还要用到右击.双击.拖动等操作,这些操作包含在ActionChains类中. 一.ActionChains类中鼠标操作 ...
- Selenium WebDriver 中鼠标事件
鼠标点击操作 鼠标点击事件有以下几种类型: 清单 1. 鼠标左键点击 Actions action = new Actions(driver);action.click();// 鼠标左键在当 ...
- Selenium+Java(九)Selenium键盘与鼠标事件
一.键盘事件 ctrl+a driver.findElement(By.id("kw")).sendKeys(Keys.CONTROL, "a"); ctrl+ ...
- Selenium WebDriver中鼠标事件
鼠标点击操作 鼠标点击事件有以下几种类型: 清单 1. 鼠标左键点击 Actions action = new Actions(driver);action.click();// 鼠标左键在当 ...
- selenium+python 移动鼠标方法
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time driver=we ...
- selenium 3.0鼠标事件 (java代码)
注意:ActionChains下相关方法在当前的firefox不工作,建议使用谷歌浏览器. public static void main(String[] args) throws Interrup ...
- 【Selenium专题】鼠标键盘事件
引用包来自selenium-java-2.23.1.jar 调用以下代码时,需要引入actions类,以java为例: import org.openqa.selenium.interactions. ...
- Python+Selenium学习--鼠标事件
场景 前景讲解了鼠标的click()事件,而我们在实际的web产品测试中,有关鼠标的操作,不仅仅只有单击,有时候还包括右击,双击,拖动等操作,这些操作包含在ActionChains类中. Action ...
随机推荐
- mysql :库操作
一 系统数据库 information_schema:虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息,列信息, 权限信息, 字符信息等. performance_schema:M ...
- Gym 100851E Easy Problemset (水题,模拟)
题意:给定 n 个裁判,然后每个都一些题目,现在要从每一个按顺序去选出 k 个题,并且这 k 个要按不递减顺序,如果没有,就用50补充. 析:就按他说的来,直接模拟就好. 代码如下: #pragma ...
- HDU - 3410 Passing the Message 单调递减栈
Passing the Message What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flo ...
- HTML学习笔记(三)样式CSS
1.样式 外部样式表(通过css文件定义样式,整体样式) 当样式需要被应用到很多页面的时候,使用外部样式表,在 head 部分<link>. <head> <link r ...
- E20190402-hm
porxy n. 代理服务器; 代表权; 代理人,代替物; 委托书; enroll v. 招收; 注册; 登记; 加入; enrollment n. 注册; 登记; 入会;
- Unity学习(六)5.x依赖打包
http://blog.sina.com.cn/s/blog_89d90b7c0102w2ox.html unity5已经封装好了接口,所以依赖打包并没有那么神秘和复杂了. 打包: 1.定义好资源的a ...
- jdbc学习day1
- PostgreSQL-4-DML数据操纵语言
1.查询语句 \h SELECT 查看SELECT语句说明 基本语法 SELECT column1, column2, columnN FROM table_name; 查询单列/多列数据 SEL ...
- Django (六) 视图 views
views 1. 视图及HttpRequest 和HttpResponse Django中的视图主要用来接受Web请求,并做出响应. 视图的本质就是一个Python中的函数 视图的响应分为两大类 1) ...
- Codeforces Round #390 (Div. 2) A
One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into sev ...