zoukankan      html  css  js  c++  java
  • selenium自动化之鼠标操作

    在做自动化测试的时候,经常会遇到这种情况,某个页面元素,你必须要把鼠标移动到上面才能显示出元素。那么这种情况,我们怎么处理呢?,selenium给我们提供了一个类来处理这类事件——ActionChains

    ActionChains可以对需要模拟鼠标操作才能进行的情况,比如单击、双击、点击鼠标右键、拖拽等等进行操作。ActionChains方法列表:

    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) ——发送某个键到指定元素

    分别使用几个小 demo能演示一下鼠标双击,鼠标移动,鼠标右键操作。

    鼠标移动demo页面截图:

    imgdemo

    1、使用鼠标移动到Write on hover按钮上方, python脚本如下:

    img鼠标移动

    代码解读一下,先定义浏览器驱动,最大化窗口,打开测试页面网址,定位到要移动到测试按钮上方,定义一个变量存储ActionChains类,使用move_to_element(element)移动到按钮上方。

    鼠标双击,右击demo页面截图

    img鼠标双击,右击demo页面截图

    2、使用鼠标双击Write on hover按钮, python脚本如下:

    img

    代码解读一下,先定义浏览器驱动,最大化窗口,打开测试页面网址,定位到要移动到测试按钮上方使用ActionChains类,调用double.click(element).perform()点击鼠标右键

    3、点击鼠标右键, python脚本如下:

    img

    代码解读一下,先定义浏览器驱动,最大化窗口,打开测试页面网址,定位到要移动到测试按钮上方,使用ActionChains类,调用context_click(element).perform()点击鼠标右键。

    转自 https://baijiahao.baidu.com/s?id=1608196589844476319&wfr=spider&for=pc

  • 相关阅读:
    [原创]mysql 错误2 系统找不到指定的文件 the template configuration file cannot be found
    SVN 钩子 允许用户修改Subversion日志的钩子脚本(转)
    Spring 注解 @Component @Service @Controller @Repository(转)
    You are running on JDK6 which comes with JAXWS 2.1 API
    特殊的日子记下特殊的日志
    hibernate中lazy的使用(转)
    css3中before和after基本用法
    string类与StringBuilder类性能比较
    NameValueCollection类总结和一个例子源码
    Flex如何创建状态States并掌握几个常用控件用法
  • 原文地址:https://www.cnblogs.com/hanfe1/p/12553494.html
Copyright © 2011-2022 走看看