且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Python、JavaScript或Robot Framework和Selenium使用部分标题文本导航到新浏览器窗口

更新时间:2022-01-12 22:30:32

使用下面的示例,自定义关键字将使用当前浏览器,然后使用模式搜索选项卡并选择它。如果结果多于或少于1个,则它将失败。

*** Settings ***
Library    SeleniumLibrary
Library    Collections

*** Test Cases ***
Select Window Using Partial Title
    Open Browser    http://www.google.com    chrome

    Click Element    link:About    CTRL
    Sleep    5s
    Title Should Be    Google


    Switch Window Using Title Pattern    Over*
    Title Should Be    Over - Google

    [Teardown]    Close Browser

*** Keywords ***


Switch Window Using Title Pattern
    [Arguments]    ${title_pattern}
    ${current_title}    Get Title            # Save the current  tab title
    ${window_titles}    Get Window Titles    # Cycles through the tabs.
    Switch Window       ${current_title}     # Restores the original tab focus

    ${match_count}      Get Match Count    ${window_titles}    ${title_pattern}

    Run Keyword If    '${match_count}'=='0'    Fail    No Browser Tabs found that meet the "${title_pattern}" pattern.
    Run Keyword If    '${match_count}'>'1'    Fail    Too many Browser Tabs found that meet the "${title_pattern}" pattern.

    ${matches}    Get Matches        ${window_titles}    ${title_pattern}   
    Switch Window    ${matches}[0]