本节重点:

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("")
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.

selenium-webdriver(python) (十五) -- 鼠标事件的更多相关文章

  1. selenium webdriver (python) 第一版PDF

    前言 如果你是一位有python语言基础的同学,又想通过python+ selenium去实施自动化,那么你非常幸运的找到了这份文档,我也非常荣幸能为你的自动化学习之路带来一丝帮助. 其实,我在sel ...

  2. selenium webdriver (python)

    selenium webdriver (python) 第一版PDF Posted on 2013-08-30 22:59 虫师 阅读(221) 评论(0) 编辑 收藏 前言 如果你是一位有pytho ...

  3. selenium webdriver (python)的基本用法一

    阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...

  4. 【转】Selenium WebDriver + Python 环境

    转自:http://www.myext.cn/webkf/a_11878.html 1. 下载必要工具及安装包 1.1 [Python开发环境] 下载并安装Python 2.7.x版本 下载地址:ht ...

  5. selenium webdriver (python) 第二版

    前言 对于大多软件测试人员来讲缺乏编程经验(指项目开发经验,大学的C 语言算很基础的编程知识)一直是难以逾越的鸿沟,并不是说测试比开发人员智商低,是国内的大多测试岗位是功能测试为主,在工作时间中,我们 ...

  6. selenium webdriver (python) 第三版

    感谢 感谢购买第二版的同学,谢谢你们对本人劳动成果的支持!也正是你们时常问我还出不出第三版了,也是你们的鼓励,让我继续学习整理本文档. 感谢乙醇前辈,第二版的文档是放在他的淘宝网站上卖的,感谢他的帮忙 ...

  7. Selenium WebDriver + Python 环境配置

    1.   下载必要工具及安装包 1.1.[Python开发环境] 下载并安装Python 2.7.x版本(当前支持2.x版本,不要下载最新的3.X的版本因为python3并非完全兼容python2) ...

  8. selenium webdriver (python)2

    selenium webdriver (python) 第二版 前言  对于大多软件测试人员来讲缺乏编程经验(指项目开发经验,大学的C 语言算很基础的编程知识)一直是难以逾越的鸿沟,并不是说测试比开发 ...

  9. Selenium WebDriver + python 自动化测试框架

    目标 组内任何人都可以进行自动化测试用例的编写 完全分离测试用例和自动化测试代码,就像写手工测试用例一下,编写excel格式的测试用例,包括步骤.检查点,然后执行自动化工程,即可执行功能自动化测试用例 ...

随机推荐

  1. C++ - 复制(copy) 和 虚复制(virtual copy) 的 区别

    复制(copy) 和 虚复制(virtual copy) 的 区别 本文地址: http://blog.csdn.net/caroline_wendy/article/details/16120397 ...

  2. highcharts 不显示X轴 Y轴 刻度

    xAxis: { tickWidth:0,        //设置刻度标签宽度 lineColor:'#ffffff',//设置坐标颜色 lineWidth:0,        //设置坐标宽度 la ...

  3. 详解spring事务属性

    Spring声明式事务让我们从复杂的事务处理中得到解脱.使得我们再也无需要去处理获得连接.关闭连接.事务提交和回滚等这些操作.再也无需要我们在与事务相关的方法中处理大量的try…catch…final ...

  4. SQLSERVER2008新增的审核/审计功能

    SQLSERVER2008新增的审核/审计功能 很多时候我们都需要对数据库或者数据库服务器实例进行审核/审计 例如对失败的登录次数进行审计,某个数据库上的DDL语句进行审计,某个数据库表里面的dele ...

  5. android precelable和Serialization序列化数据传输

    一 序列化原因: 1.永久性保存对象,保存对象的字节序列到本地文件中:2.通过序列化对象在网络中传递对象:3.通过序列化在进程间传递对象. 二 至于选取哪种可参考下面的原则: 1.在使用内存的时候,P ...

  6. node.js-session问题

    在使用express使用session时发现怎么使用session都是undefined最后发现 app.use(express.cookieParser()); app.use(express.se ...

  7. hadoop使用问题

    前提 环境 ubuntu 安装hadoop 已经有一段时间 1.启动的时候提示 Connection reset by peer 这个查看日志,里面有说 ssh里面某个文件的权限太大 这个ssh里修改 ...

  8. SQL SERVER 内存学习系列(一)

    最近帮客户解决发布订阅的问题时,突然遇到这样一个问题发布订阅中报下面的错误,另外执行alter table 操作时也会报错 : 问题很奇怪发布订阅和CLR有什么关系?memtoleave内存是个啥?回 ...

  9. nginx(3、负载均衡)

    当业务系统需要配置集群时,会用到nginx的负载均衡功能.nginx提供如下几种: 1.轮询(默认):将不同的请求随机分配给配置的服务器,若出现宕机,则自动切换:轮询可配置weight值,即权重,权重 ...

  10. 字体大小自适应纯css解决方案

    viewpoint css3提供了一些与当前viewpoint相关的元素,vw,vh,vim等. “viewpoint” = window size vw = 1% of viewport width ...