• selenium之SwitchAPI与SelectAPI


    Switch

    我们在UI自动化测试时,总会出现新建一个tab页面、弹出一个浏览器级别的弹框或者是出现一个iframe标签,这时我们用WebDriver提供的Api接口就无法处理这些情况了。需要用到Selenium单独提供的模块switch_to模块

    SwitchToWindows  获取浏览器句柄

    handles = driver.window_handles
     
    # SwitchToWindows接受浏览器TAB的句柄
    driver.switch_to.window(handles[-1])  #切换至最后一个Tab页

    SwitchToFrame  定位iframe

     1 # SwitchToFrame支持id、name、frame的element
     2  
     3 # 接受定位到的iframe的Element,这样就可以通过任意一种定位方式进行定位了
     4 framElement = driver.find_element_by_css_selector('[src="/new-index/"]')  #通过iframe里的元素去定位iframe的层
     5 driver.switch_to.frame(frameElement)
     6  
     7 # 通过fame的name、id属性定位
     8 driver.switch_to.frame('top-frame')
     9  
    10 # 当存在多层iframe嵌套时,需要一层一层的切换查找,否则将无法找到
    11 driver.switch_to.frame('top-frame')
    12 driver.switch_to.frame('baidu-frame')
    13  
    14 # 跳转到最外层的页面
    15 driver.switch_to.default_content()
    16  
    17 # 多层Iframe时,跳转到上一层的iframe中
    18 driver.switch_to.parent_frame()

    SwitchToAlert  定位操作alert

     1 # 也可以通过Webdriver的switch_to来调用
     2  
     3 # 点击确认按钮
     4 driver.switch_to.alert.accept()
     5  
     6 # 取消按钮 
     7 driver.switch_to.alert.dismiss()
     8  
     9  
    10 # 如果alert上有文本框时,可以输入文字。(注: 没遇到过)
    11 driver.switch_to.alert.send_keys()
    12  
    13 # 返回Alert上面的文本内容
    14 text = driver.switch_to.alert.text

     Select   下拉框

    在UI自动化测试过程中,经常会遇到一些下拉框,如果我们基于Webdriver操作的话就需要click两次,而且很容易出现问题,实际上Selenium给我们提供了专门的Select(下拉框处理模块)。

    Select操作

     1 from selenium.webdriver.support.select import Select
     2 
     3 # 通过select选项的索引来定位选择对应选项(从0开始计数)
     4 Select(s).select_by_index(5)
     5  
     6 # 通过选项的value属性值来定位
     7 Select(s).select_by_value('2')
     8  
     9 # 通过选项的文本内容来定位
    10 Select(s).select_by_visible_text('成都')
    11  
    12 # 返回第一个选中的optionElement对象
    13 Select(s).first_selected_option
    14  
    15 # 返回所有选中的optionElement对象
    16 Select(s).all_selected_options
    17  
    18 # 取消所有选中的option
    19 Select(s).deselect_all()
    20  
    21 # 通过option的index来取消对应的option
    22 Select(s).deselect_by_index(1)
    23  
    24 # 通过value属性,来取消对应option
    25 Select(s).deselect_by_value('')
    26  
    27 # 通过option的文本内容,取消对应的option
    28 Select(s).deselect_by_visible_text('')
  • 相关阅读:
    iframe应用 相互之间发送消息 postMessage
    function angular.bootstrap()
    总结
    1-angular.bind
    jQuery treeTable v 1.4.2
    声明了一个模块和一个控制器AngularJS的处理过程
    angularJs $templateCache
    $.fn.zTree 的使用
    Java异常throws与throw的区别
    Pom报错
  • 原文地址:https://www.cnblogs.com/yanyan-/p/11143738.html
Copyright © 2020-2023  润新知