• Selenium-ActionChainsApi接口详解


    ActionChains

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

    引入方式

    from selenium.webdriver.common.action_chains import ActionChains

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

    move_to_element(鼠标移动到某个元素上)

    # 连贯操作
    from selenium.webdriver.common.action_chains import ActionChains
    #鼠标悬浮在element上后才出现element2
    element = driver.find_element_by_css_selector('#w>#a')
    element2 = driver.find_element_by_css_selector('#dis1')
    
    action = ActionChains(driver)
    action.move_to_element(element).click(element2).perform()

    对于隐藏的元素,即display='none'时,还可以通过dom来操作JS改变display样式,使得元素不再隐藏

    #第二种执行js
    js = "document.getElementById('dis1').style.display='';"
    driver.execute_script(js)
    
    time.sleep(2)
    driver.find_element_by_css_selector('#dis1').click()

    drag_and_drop(拖拽操作)

    # 将source元素拖放至target元素处,参数为两个elementObj
    ActionChains(driver).drag_and_drop(source=source,target=target)
    
    #例子
    from selenium.webdriver.common.action_chains import ActionChains
    #从s1元素位置拖到t1元素位置
    s1 = driver.find_element_by_css_selector('#dragger')
    t1 = driver.find_element_by_css_selector('#i1')
    ActionChains(driver).drag_and_drop(s1,t1).perform()
    
     
    # 将一个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()

    click

    # 单击事件,可接受elementObj
    ActionChains(driver).click()
     
    # 双击事件,可接受elementObj
    ActionChains(driver).double_click()
     
    # 点击鼠标右键
    ActionChains(driver).context_click()
     
    # 点击某个元素不松开,接收elementObj
    ActionChains(driver).click_and_hold()
     
    # # 某个元素上松开鼠标左键,接收elementObj
    ActionChains(driver).release()
  • 相关阅读:
    [ 低危 ] mt网CRLF
    mysql之字段的修改,添加、删除,多表关系(外键),单表详细操作(增删改)
    mysql 之编码配置、引擎介绍、字段操作、数据类型及约束条件
    Navicat Premium永久激活方式
    centos 用户名密码忘记了怎么办?
    并发编程总结
    初识mysql
    线程queue、线程进程池,协程
    python解释器
    线程全局修改、死锁、递归锁、信号量、GIL以及多进程和多线程的比较
  • 原文地址:https://www.cnblogs.com/bugoobird/p/13225717.html
Copyright © 2020-2023  润新知