自动化学习【4】- ActionChains基础操作

一、引入依赖包:
from selenium.webdriver.common.action_chains import ActionChains as Action
前言:获取 ActionChains()类的实例:
action = Action(driver)
二、ActionChains 基础操作:
1. 所有 ActionChains 的事件操作最后都需要执行 perform() 才会生效
# 例如执行一个点击操作,也必须执行 perform 方法才可以执行
action.click(element).perform()
2. 鼠标左键点击:click(to_element)
action.click(element).perform()
3. 鼠标右键点击:context_click(to_element)
action.context_click(element).perform()
4. 鼠标左键双击:double_click(to_element)
action.double_click(element).perform()
5. 鼠标左键长按:click_and_hold(to_element)
action.click_and_hold(element).perform()
6. 鼠标移动到页面元素之上:move_to_element(to_element)
action.move_to_element(element).perform()
7. 鼠标移动到页面坐标位置: move_by_offset(xoffset, yoffset)
action.move_by_offset(5,10).perform()	
8. 鼠标从某个元素位置开始移动鼠标坐标:move_to_element_with_offset(to_element, xoffset, yoffset)
action.move_to_element_with_offset(element, 5 , 10).perform()
9. 释放元素上的鼠标控制:release(on_element)
action.release(element).perform()
10、指定时间内暂停所有操作:pause() 单位:秒
action.pause(5).perform()	# 中断操作 5 秒
11 、拖动起始元素到目标元素之上:action.drag_and_drop( source, target )
# 获取 element
source = driver.find_element_by_name('source')
target = driver.find_element_by_name('target')

# 执行拖动操作
action.drag_and_drop(source, target).perform()
12、拖动元素到对应坐标之上: drag_and_drop_by_offset( source, xoffset, yoffset )
# 获取 element
source = driver.find_element_by_name('source')

# 执行拖动操作
action.drag_and_drop_by_offset(source , 5 ,10 ).perform()
13、 针对键盘操作
action.key_down(self, value, element=None)按下键盘某个键位,不释放
action.key_up(self, value, element=None)释放键盘某个键位
a. Keys 属性介绍:
# 引入依赖包
from selenium.webdriver.common.keys import Keys
Keys.CONTROLCtrl 键
Keys.ALTAlt 键
Keys.SHIFTShift 键
Keys.TABTab 键
Keys.ESCAPEEsc 键
Keys.ENTEREnter 键
Keys.DELETEDEL 键
b. 使用
# 模拟键盘 Ctrl + a  全选操作
action.key_down(Keys.CONTROL).send_keys('a')

# 释放 Ctrl 键
action.key_up(Keys.CONTROL)

# 执行操作
action.perform()
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值