基于《Selenium 2自动化测试实战》的学习笔记(8)—— 鼠标事件

ActionChains 类鼠标操作的常用方法:

context_click() 右击
double_click() 双击
drag_and_drop()拖动
move_to_element() 鼠标悬停在一个元素上
click_and_hold() 按下鼠标左键在一个元素上


模拟鼠标右键

假如一个web 应用的列表文件提供了右击弹出快捷菜单的的操作。可以通过context_click()方法模拟鼠标右键,参考代码如下:

#引入ActionChains 类
from selenium.webdriver.common.action_chains import ActionChains
...
#定位到要右击的元素
right =driver.find_element_by_xpath("xx")
#对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(right).perform()
....

ActionChains(driver)

driver: wedriver 实例执行用户操作。
ActionChains 用于生成用户的行为;所有的行为都存储在actionchains 对象。通过perform()执行存储的行为。

perform()

执行所有ActionChains 中存储的行为。perfrome()同样也是ActionChains类提供的的方法,通常与ActionChains()配合使用。


鼠标双击操作

double_click(on_element)

#引入ActionChains 类
from selenium.webdriver.common.action_chains import ActionChains
...
#定位到要双击的元素
double =driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标双击操作
ActionChains(driver).double_click(double).perform()

鼠标拖放操作

drag_and_drop(source, target)

在源元素上按下鼠标左键,然后移动到目标元素上释放。

source: 鼠标按下的源元素。

target: 鼠标释放的目标元素。

#引入ActionChains 类
from selenium.webdriver.common.action_chains import ActionChains
...
#定位元素的原位置
element = driver.find_element_by_name("xxx")
#定位元素要移动到的目标位置
target = driver.find_element_by_name("xxx")
#执行元素的移动操作
ActionChains(driver).drag_and_drop(element, target).perform()

鼠标移动上元素上

move_to_element()

模拟鼠标移动到一个元素上。

#引入ActionChains 类
from selenium.webdriver.common.action_chains import ActionChains
...
#定位到鼠标移动到上面的元素
above = driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标移动到上面的操作
ActionChains(driver).move_to_element(above).perform()

按下鼠标左键

click_and_hold()

按住鼠标左键在一个元素。

#引入ActionChains 类
from selenium.webdriver.common.action_chains import ActionChains
...
#定位到鼠标按下左键的元素
left=driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标左键按下的操作
ActionChains(driver).click_and_hold(left).perform()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值