selenium:鼠标模拟操作ActionChains

ActionChains

1.导入ActionChains包

python 复制代码
from selenium.webdriver import ActionChains
  1. 执行原理

调用ActionChains的方法时,不会立即执行,而是将所有的操作,按顺序存放在一个队列里,当你调用perform()方法时,队列中的事件会依次执行

  1. 基本用法:
  • 生成一个动作action=ActionChains(driver)
  • 动作添加方法1 action.方法1
  • 动作添加方法2 action.方法2
  • 调用perform()方法执行(action.perform())
  1. 具体写法

链式写法

  • ActionChains(driver).move_to_element(element).click(element)perform()

分布写法:

  • actions = ActionChains(driver)
  • actions.move to element(element)
  • actions.click(element)
  • actions.perform()

示例:

python 复制代码
    def test_actionchains(self):
        self.driver.get("https://www.baidu.com/")
        element = self.driver.find_element('xpath',"//*[@id='s-top-left']/a[1]")
        action = ActionChains(self.driver)
        action.click(element)
        action.perform()
  1. 不同操作对应不同的方法:
  • click(on_element=None) #单击鼠标左键
  • click_and_hold(on_element=None) #点击鼠标左键,不松开
  • context_click(on_element=None) #点击鼠标右键
  • double_click(on_element=None) #双击鼠标左键
  • drag_and_drop(source, target) #拖拽到某个元素,然后松开
  • drag_and_drop_by_offset(source, xoffset, yoffset) #拖拽到某个坐标然后松开
  • key_down(value, element=None) #按下某个键盘上的键
  • key_up(value, element=None) #松开某个键
  • move_by_offset(xoffset, yoffset) #鼠标从当前位置移动到某个坐标
  • move_to_element(to_element) #鼠标移动到某个元素
  • move_to_element_with_offset(to_element, xoffset, yoffset) #移动到距某个元素多少距离的位置
  • perform() # 执行
  • release(on_element=None) #在某个元素位置松开鼠标左键
  • send_keys(*keys_to_send) #发送某个键到当前焦点的元素
  • send_keys_to_element(element, *keys_to_send) #发送某个键到指定元素

示例

python 复制代码
class TestClick:

    def setup(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)#隐式等待
        self.driver.get('https://ip:port/')

    def test_click(self):
        e = self.driver.find_element(by='xpath',value='//*[@id="root"]/main/div[2]/div[4]/div[2]/div[1]/div/div[1]/div[2]/span')
        ActionChains(self.driver).move_to_element(e).click(e).perform()
相关推荐
有风南来17 小时前
算术图片验证码(四则运算)+selenium
自动化测试·python·selenium·算术图片验证码·四则运算验证码·加减乘除图片验证码
程序员三藏1 天前
如何使用Jmeter进行压力测试?
自动化测试·软件测试·python·测试工具·jmeter·测试用例·压力测试
编程乐学(Arfan开发工程师)1 天前
42、响应处理-【源码分析】-浏览器与PostMan内容协商完全适配
java·spring boot·后端·测试工具·lua·postman
集成显卡1 天前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
互联网杂货铺1 天前
完美搭建appium自动化环境
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
测试老哥2 天前
Jmeter如何进行多服务器远程测试?
自动化测试·软件测试·功能测试·测试工具·jmeter·测试用例·性能测试
鱼鱼说测试2 天前
postman基础
测试工具·postman
程序员杰哥2 天前
Postman常见问题及解决方法
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·postman
小堃学编程2 天前
Selenium常用函数介绍
selenium·测试工具
羊米奇2 天前
selenium-自动更新谷歌浏览器驱动
selenium·谷歌浏览器驱动更新