web自动化测试(十二)控件交互进阶

Actions

  • 官方文档:https://selenium-python.readthedocs.io/api.html
  • ActionChains: 执行PC端的鼠标点击、双击、右击、拖拽等事件
  • TouchActions:模拟执行PC端和移动端的点击、滑动、拖拽、多点触控等多种手势控制操作

ActionChains两种写法

  1. 链式写法
ActionChains(driver).move_to_element(element).click(element).perform()
  1. 分布写法
actions = ActionChains(driver)
actions.move_to_element(element)
actions.click(element)
actions.perform()

ActionChains API

actions = ActionChains(driver)
# 点击
actions.click(element)
# 双击
actions.double_click(element)
# 右击
actions.context_click(element)
# 鼠标移动到某个元素上
actions.move_to_element(element)
# 拖拽
actions.drag_and_drop(drag_element, drop_element)
# 按住不放
actions.click_and_hold(element)
# 模拟按键
actions.send_keys(Keys.BACK_SPACE)
# 按下抬起
actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL)
# 释放
actions.release()

TouchActions 手势控制

注意:selenium 4.x版本已经不再支持
from selenium.webdriver import TouchActions

  • tap -- 在指定元素敲击
  • double_tap -- 在指定元素上双击
  • tap_and_hold -- 在元素上点击但不释放
  • move -- 手势移动指定偏移(未释放)
  • release -- 释放手势
  • scroll -- 手势点击并滑动
  • scroll_from_element -- 从某个元素位置开始手势点击并滚动(向下滑动为负数,向上滑动为正数)
  • long_press -- 长按
  • flick -- 手势滑动
  • flick_element -- 从某个元素位置开始手势滑动
  • perform -- 执行
posted @ 2022-05-30 23:19  小小滴人a  阅读(72)  评论(0编辑  收藏  举报