qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

selenium + python 鼠标事件

十、鼠标事件
本章重点:
ActionChains 类
context_click()
右击
double_click()
双击
drag_and_drop()
拖动
测试的产品中有一个操作是右键点击文件列表会弹出一个快捷菜单,可以方
便的选择快捷菜单中的选择对文件进行操作(删除、移动、重命名),之前学习
元素的点击非常简单:
driver.find_element_by_id(“xxx”).click()
博客园—虫师
http://fnng.cnblogs.com
26
那么鼠标的双击、右击、拖动等是否也是这样的写法呢?例如右击:
driver.find_element_by_id(“xxx”).context_click()
经过运行脚本得到了下面的错误提示:
AttributeError: 'WebElement' object has no attribute 'context_click'
提示右点方法不属于 webelement 对象,通过查找文档,发现属于
ActionChains 类,但文档中没有具体写法。这里要感谢 北京-QC-rabbit 的指
点,其实整个 python+selenium 学习过程都要感谢 北京-QC-rabbit 的指点。
10.1、鼠标右键
下面介绍鼠标右键的用法,以快播私有云为例:
#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()
博客园—虫师
http://fnng.cnblogs.com
27
'''
#你也可以使用三行的写法,但我觉得上面两行写法更容易理解
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 类之前,要先将包引入。
右击的操作会了,下面的其它方法比葫芦画瓢也能写出来。
10.2、鼠标双击
鼠标双击的写法:
#定位到要双击的元素
qqq =driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标双击操作
ActionChains(driver).double_click(qqq).perform()
10.3、鼠标拖放
鼠标拖放操作的写法:
#定位元素的原位置
element = driver.find_element_by_name("source")
#定位元素要移动到的目标位置
target =
driver.find_element_by_name("target")
博客园—虫师
http://fnng.cnblogs.com
28
#执行元素的移动操作
ActionChains(driver).drag_and_drop(element, target).perform()

posted on 2014-04-21 19:17 顺其自然EVO 阅读(2545) 评论(0)  编辑  收藏 所属分类: 自动化测试python


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜