Actions操作

Actions
Action Chains:执行PC端的鼠标点击、双击,右键,拖拽等事件,对h5页面无效
  1. 执行原理:调用ActionChains方法时,不会立即执行,而是所有的操作,按照顺序放在一个队列中,当调用perform时,队列中的事件依次执行
  2. 基本用法
# 生成一个动作
action = ActionChains(driver)

# 动作添加方法1
action.方法1

# 动作添加方法2
action.方法2

# 调用perform()方法执行
action.perform()
  1. 具体写法
# 链式写法
ActionChains(driver).move_to_element(element).click(element).perform()
# 分布写法
action = ActionChains(driver)
action.move_to_element(element)
action.click(element)
action.perform()
  1. 事件栗子
# 点击
action.click(element)
# 双击
action.double_click(element)
# 右击
action.context_click(element)
# 移动到某个元素
action.move_to_element(element)
# 拖拽元素1到元素2上
action.drag_and_drop(element1,element2)
# 拖拽元素1到元素2上
action.click_and_hold(drag_element).release(drop_element)
# 等待操作
action.pause(1)
  1. 模拟按键:模拟按键方法很多,可以使用win32api,能用SendKeys来实现,也可以用selenium的WebElement对象的send_keys()方法实现
# 删除键
action.send_keys(Key.BACK_SPACE)  # 更多看Key定义文档
or action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL)
Touch Actions:模拟PC和移动端的点击,滑动,拖拽,多点触控以及模拟手势操作,可以操作h5页面
  1. 手势控制
tap --- 在指定元素上敲击
double_tap  ---  在指定元素上双敲击
tap_and_hold   ---  在指定元素上点击但不释放
move  ---  手势移动到指定偏移(未释放)
release   ---  释放手势
scroll   ---  手势点击滚动
scroll_from_element   ---  从某个元素位置开始手势点击并滚动(向下滑动为负数,向上滑动为正数)
long_press   ---  长按元素
flick  ---  手势滑动
flick_element   ---  从某个元素开始手势手势滑动(向上滑动为负数,向下滑动为正数)
perform    --- 执行
  1. 举个栗子
action = TouchChains(driver)
action.tap(element)   # 在指定元素上敲击
action.scroll_from_element(element,0,10000)  # 页面滑动到底部
action.perform()
# 报错注意:
option = webdriver.ChromeOptions()
option.add_experimental_option('w3c', False)
driver = webdriver.Chrome(option=option)
表单
# 同登录操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值