zoukankan      html  css  js  c++  java
  • Selenium-ActionChainsApi接口详解

    转: http://www.imdsx.cn/index.php/2017/08/01/act/

    ActionChains

    UI自动化测试过程中,经常遇到那种,需要鼠标悬浮后,要操作的才会元素出现的这种场景,那么我们就要模拟鼠标悬浮到某一个位置,做一系列的连贯操作,Selenium给我们提供了ActionChains模块。

    引入方式

        
    from selenium.webdriver.common.action_chains import ActionChains

    实际上ActionChains这个模块的实现的核心思想就是,当你调用ActionChains的方法时,不会立即执行,而是会将所有的操作按顺序存放在一个List里,当你调用perform()方法时,队列中的时间会依次执行。(注:推荐一个尺子工具,MeasulerIt)

    drag_and_drop

    # 将source元素拖放至target元素处,参数为两个elementObj
    ActionChains(driver).drag_and_drop(source=source,target=target)
     
    # 将一个source元素 拖动到针对source坐上角坐在的x y处 可存在负宽度的情况和负高度的情况
    ActionChains(driver).drag_and_drop_by_offset(source, x, y)
     
    # 这种也是拖拽的一种方式,都是以源元素的左上角为基准,移动坐标
    ActionChains(driver).click_and_hold(dom).move_by_offset(169,188).release().perform()

    move_to_element

    # 鼠标移动到某一个元素上,结束elementObj
    ActionChains(driver).move_to_element(e)
     
    # 鼠标移动到制定的坐标上,参数接受x,y
    ActionChains(driver).move_by_offset(e['x'],e['y'])
     
    例:
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('http://ui.imdsx.cn/uitester/')
    time.sleep(2)
    driver.execute_script('document.body.scrollTop=0')
    time.sleep(1)
    a = driver.find_element_by_id('a').location
    dis = driver.find_element_by_id('dis1')
    ActionChains(driver).move_by_offset(a['x'],a['y']).double_click(dis).perform()

    click

    # 单击事件,可接受elementObj
    ActionChains(driver).click()
     
    # 双击事件,可接受elementObj
    ActionChains(driver).double_click()
     
    # 点击鼠标右键
    ActionChains(driver).context_click()
     
    # 点击某个元素不松开,接收elementObj
    ActionChains(driver).click_and_hold()
     
    # # 某个元素上松开鼠标左键,接收elementObj
    ActionChains(driver).release()

    key_down与key_up

    有时我们需要模拟键盘操作时,那么就需要用到ActionChains中的key操作了,提供了两个方法,key_down与key_up,模拟按下键盘的某个键子,与松开某个键子,接收的参数是按键的Keys与elementObj。可以与send_keys连用(例:全选、复制、剪切、粘贴)

    # key_down 模拟键盘摁下某个按键 key_up 松开某个按键,与sendkey连用完成一些操作,每次down必须up一次否则将出现异常
    ActionChains(driver).key_down(Keys.CONTROL,dom).send_keys('a').send_keys('c').key_up(Keys.CONTROL)
        .key_down(Keys.CONTROL,dom1).send_keys('v').key_up(Keys.CONTROL).perform()

    Keys 实际是Selenium提供的一个键盘事件模块,在模拟键盘事件时需要导入Keys模块

    引入路径

       
    from selenium.webdriver.common.keys import Keys
  • 相关阅读:
    Checking Types Against the Real World in TypeScript
    nexus pip proxy config
    go.rice 强大灵活的golang 静态资源嵌入包
    几个golang 静态资源嵌入包
    rpm 子包创建学习
    Rpm Creating Subpackages
    ava 类似jest snapshot 功能试用
    ava js 测试框架基本试用
    The Architectural Principles Behind Vrbo’s GraphQL Implementation
    graphql-compose graphql schema 生成工具集
  • 原文地址:https://www.cnblogs.com/shmily2018/p/9278677.html
Copyright © 2011-2022 走看看