python selenium --鼠标事件
转自:http://www.cnblogs.com/fnng/p/3288444.html
本节重点:
ActionChains 类
- context_click() 右击
- double_click() 双击
- drag_and_drop() 拖动
测试的产品中有一个操作是右键点击文件列表会弹出一个快捷菜单,可以方便的选择快捷菜单中的选择对文件进行操作(删除、移动、重命名),之前学习元素的点击非常简单:
driver.find_element_by_id(“xxx”).click()
那么鼠标的双击、右击、拖动等是否也是这样的写法呢?例如右击:
driver.find_element_by_id(“xxx”).context_click()
经过运行脚本得到了下面的错误提示:
AttributeError: 'WebElement' object has no attribute 'context_click'
提示右点方法不属于webelement 对象,通过查找文档,发现属于ActionChains 类,但文档中没有具体写法。这里要感谢 北京-QC-rabbit 的指点,其实整个python+selenium 学习过程都要感谢 北京-QC-rabbit 的指点。
下面介绍鼠标右键的用法,以快播私有云为例:

#coding=utf-8 from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time driver = webdriver.Firefox()
driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F") #登陆快播私有云
driver.find_element_by_id("user_name").send_keys("username")
driver.find_element_by_id("user_pwd").send_keys("123456")
driver.find_element_by_id("dl_an_submit").click()
time.sleep(3) #定位到要右击的元素
qqq =driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
#对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(qqq).perform() '''
#你也可以使用三行的写法,但我觉得上面两行写法更容易理解
chain = ActionChains(driver)
implement = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
chain.context_click(implement).perform()
''' time.sleep(3) #休眠3秒
driver.close()

这里需要注意的是,在使用ActionChains 类之前,要先将包引入。
右击的操作会了,下面的其它方法比葫芦画瓢也能写出来。
鼠标双击的写法:
#定位到要双击的元素
qqq =driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标双击操作
ActionChains(driver).double_click(qqq).perform()
鼠标拖放操作的写法:

#定位元素的原位置
element = driver.find_element_by_name("source")
#定位元素要移动到的目标位置
target = driver.find_element_by_name("target") #执行元素的移动操作
ActionChains(driver).drag_and_drop(element, target).perform()

ActionChains 类不仅仅是只包含了上面的三个方法,下面将方法列出:
class ActionChains(driver)
driver:The WebDriver instance which performs user actions.
Generate user actions. All actions are stored in the ActionChains object. Call perform() to fire stored actions.
– perform()
Performs all stored actions.
– click(on_element=None)
Clicks an element.
on_element:The element to click. If None, clicks on current mouse position.
– click_and_hold(on_element)
Holds down the left mouse button on an element.
on_element:The element to mouse down. If None, clicks on current mouse position.
– context_click(on_element)
Performs a context-click (right click) on an element.
on_element:The element to context-click. If None, clicks on current mouse position.
– double_click(on_element)
Double-clicks an element.
on_element:The element to double-click. If None, clicks on current mouse position.
– drag_and_drop(source, target)
Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
source:The element to mouse down.
target: The element to mouse up.
– key_down(key, element=None)
Sends a key press only, without releasing it. Should only be used with modifier keys (Control, Alt andShift).
key:The modifier key to send. Values are defined in Keys class.
element:The element to send keys. If None, sends a key to current focused element.
– key_up(key, element=None)
Releases a modifier key.
key:The modifier key to send. Values are defined in Keys class.
element:The element to send keys. If None, sends a key to current focused element.
– move_by_offset(xoffset, yoffset)
Moving the mouse to an offset from current mouse position.
xoffset:X offset to move to.yoffset:Y offset to move to.
– move_to_element(to_element)
Moving the mouse to the middle of an element.
to_element: The element to move to.
– move_to_element_with_offset(to_element, xoffset, yoffset)
Move the mouse by an offset of the specificed element. Offsets are relative to the top-left corner of the
element.
to_element: The element to move to.xoffset:X offset to move to.yoffset:Y offset to move to.
– release(on_element)
Releasing a held mouse button.
on_element:The element to mouse up.
– send_keys(*keys_to_send)
Sends keys to current focused element.
keys_to_send:The keys to send.
– send_keys_to_element(self, element,*keys_to_send):
Sends keys to an element.
element:The element to send keys.keys_to_send:The keys to send.
python selenium --鼠标事件的更多相关文章
- 9 Python+Selenium鼠标事件
[环境信息] python3.6+Selenium3.0.2+Firefox50.0+win7 [ActionChains类鼠标事件的常用方法] 1.右击:context_click() 2.双击:d ...
- python+selenium 鼠标事件操作
一.前言 除了可以使用 click( ) 来模拟鼠标的单击操作,现在Web产品中还提供了更丰富的鼠标交互方式,例如鼠标右键.双击.悬停.拖动等功能,在WebDriver中,将这些关于鼠标操作的方法都封 ...
- python+selenium_鼠标事件
引言--在实际的web产品测试中,对于鼠标的操作,不单单只有click(),有时候还要用到右击.双击.拖动等操作,这些操作包含在ActionChains类中. 一.ActionChains类中鼠标操作 ...
- selenium 鼠标事件操作
1.操作鼠标事件的类:ActionChains perform() 执行所有ActionChains中存储的行为 context_click() 右击 double_click() 双击 d ...
- Python+selenium鼠标、键盘事件
鼠标操作 现在的Web产品提供了丰富的鼠标交互方式,例如鼠标右击.双击.悬停.甚至是鼠标拖动等功能,在Webdriver中,将这些关于鼠标操作的方法封装在ActionChains类提供. 1.鼠标右击 ...
- python selenium鼠标键盘操作(ActionChains)
用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件--ActionChains sele ...
- python + selenium相关事件和元素定位
女友由于工作上的失误,将公司RDM中的某一字段的2000条数据给删除了.....就算是重新添加字段,但是与其他数据的关联性已经不在了.由于每天的数据修改量大,有关部门不愿意恢复数据库,因此只能一条条的 ...
- python selenium ---键盘事件
转自:http://www.cnblogs.com/fnng/p/3258946.html 本节重点: l 键盘按键用法 l 键盘组合键用法 l send_keys() 输入中文运行报错问题 键盘按键 ...
- Python+Selenium - 鼠标操作
鼠标操作类:action_chains模块的ActionChains类 使用组成:操作 + 执行(perform()) 导入代码 from selenium.webdriver.common.acti ...
随机推荐
- 数组中的forEach和map的区别
大多数情况下,我们都要对数组进行遍历,然后经常用到的两个方法就是forEach和map方法. 先来说说它们的共同点 相同点 都是循环遍历数组中的每一项 forEach和map方法里每次执行匿名函数都支 ...
- 添加HP消息队列
Date: 20140210 Auth: Jin 两台服务器 120 HTTPSQS 236 HTTPPROXY 1.add host $ jump 120 #vim /etc/hosts 192.1 ...
- SQL SERVER 2008 数据库置疑处理办法
1.修改数据库为紧急模式 ALTER DATABASE Zhangxing SET EMERGENCY 2.使数据库变为单用户模式 ALTER DATABASE Zhangxing SET SINGL ...
- ubuntu系统使用dnw下载程序
转:http://blog.chinaunix.net/uid-22030783-id-3350840.html 获得dnw4linux.tar.bz2 源码包,可在xcembed论坛下载:http: ...
- spring的@Async异步使用
pring的@Async功能,用的时候一定要注意: 1.异步方法和调用类不要在同一个类中. 2.xml里需要加入这一行 <task:annotation-driven/> 下面的可以直接粘 ...
- arcgis10.2转shp文件中文乱码问题解决方案
从ArcGIS的数据源入手,自己升级ArcGIS for Desktop到10.2.0,然后用该版本ArcGIS软件重新导出数据,竟然还是乱码.经咨询ArcGIS技术支持,技术支持说必须保证shp文件 ...
- 【maven】ecplise新建maven项目 报错Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin
在ecplise上新建maven项目 报错: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resourc ...
- nyoj42一笔画问题(欧拉图)
这题事实上说不上难度4.仅仅是一个简单的欧拉图推断,给一些点,首先推断连通性.假设不连通.就谈不上是欧拉图. 假设是连通的,在推断度的数目,每一个顶点都是偶数个或者仅仅有两个是奇数个.这样就能够构成欧 ...
- Android 将ARGB图片转换为灰度图
思路如下: 1.读取or照相,得到一张ARGB图片. 2.转化为bitmap类,并对其数据做如下操作: A通道保持不变,然后逐像素计算:X = 0.3×R+0.59×G+0.11×B,并使这个像素的值 ...
- 在执行migrate的时候出现问题(错误见末尾): django.db.utils.OperationalError: (1045, "Access denied for user ‘ODBC‘@‘localho st‘ (using password: YES)")
Python框架之Django的数据库 在执行migrate的时候出现问题(错误见末尾) django.db.utils.OperationalError: (1045, "Access d ...