RobotFramework:切换页面和Frame框架

时间:2023-03-09 09:34:08
RobotFramework:切换页面和Frame框架

切换页面主要有以下两种情况

  1. 在浏览器上打开多个窗口(Windows),在窗口内切换
  2. 打开多个浏览器(Browser),在多个浏览器内切换

1. 切换窗口

  • 该操作适用于:打开两(多)个窗口页面,在打开的窗口页面进行切换操作
  • 主要需要用Select Windows 和Close Windows来处理弹出的窗口

代码如下

  import Selenium2Library
  import Collections

  Open Browser     https://www.baidu.com       chrome
  Maximize Browser Window
  Input Text       kw         ss
  Click Element    su    #点击搜索框
  sleep   3
  Click Element    xpath=.//*[@id='1']/h3/a[1]    #选择第一个页面
  #获取窗口的title
  ${titles2}      Get Window Titles
  ${titles2}    Get From List    ${titles}    1
  ${titles1}    Get From List    ${titles}    0
  Log      ${titles}
  log       ${titles2}
  log      ${titles1}
  #切换到第二个窗口
  Select Window    title=${titles2}
  #切换回第一个窗口,即搜索页面的那个窗口
  Select Window    title=${titles1}

RobotFramework:切换页面和Frame框架


2.切换浏览器

  • 该操作适用于:在多个浏览器之间切换进行切换操作
  • 主要用Open Browser 、Switch Browser 和Close Browser来处理

代码引用

    import Selenium2Library

    ${id1}    Open Browser    https://www.baidu.com    chrome    2nd_conn
    Maximize Browser Window
    Input Text    id=kw    你好
    Click Element    id=su
    #**新打开浏览器**
    ${id2}    Open Browser    http://www.jianshu.com/users/8e79ee3c829b/timeline    chrome
    Maximize Browser Window
    #**重新创建一个句柄**
    ${service args}=    Create List    --proxy=localhost:8888
    ${id3}    Create Webdriver    Chrome    service_args=${service args}
    Go To    https://www.baidu.com
    Wait Until Page Contains Element    id=kw
    Input Text     id=kw      hello
    #**切换浏览器**切换到第一次打开的 这里也可以用索引 1;也可以用2nd_conn
    Switch Browser    ${id1}
    Wait Until Page Contains Element    //div[@id='content_left']/div/h3/a
    Capture Page Screenshot
    #**切换浏览器**切换到第二次打开的
    Switch Browser    ${id2}
    Capture Page Screenshot
    #可在该页面进行操作。。。
    #**切换浏览器**切换到第三次打开的
    Switch Browser    ${id3}
    Capture Page Screenshot

关键字Create Webdriver 可以内置参数

RobotFramework:切换页面和Frame框架

遇到Frame框架

Select Frame 可以通过id name等元素进行定位,若frame/iframe框架代码如下图所示: 
RobotFramework:切换页面和Frame框架

则无法用id 或者name等系列参数进行定位时,可以采用以下方式进行定位:

Select Frame      Xpath=//*[contains(@src, 'http://10.0.20.53:8080/dwp/index.html')]

具体使用代码如下:

Wait Until Element Is Enabled     Xpath=//*[contains(@src,'http://10.0.20.53:8080/dwp/index.html')]

Select  Frame         Xpath=//*[contains(@src, 'http://10.0.20.53:8080/dwp/index.html')] 

Unselect  Frame

最后使用Unselect Frame退出frame/iframe框架