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

nhrcpt

在测试过程中,我们需要单击新标签页中打开网页的链接。在某些情况下,由于标题文本中包含多个空格或制表符,我们看到测试失败。这类标题的例子之一是-

"Looking for more information about US?${SPACE}${SPACE}Find out more from ... Get your questions answered."

我正在尝试使用部分文本“查找有关美国的更多信息”而不是使用整个标题文本切换到窗口。但是到目前为止,还没有找到一种方法来做到这一点。

我当前的代码如下:

Test for "Frequently Asked Question"" Link at the bottom panel
    Hide Promo Carousel
    execute javascript  window.scrollTo(0,document.body.scrollHeight)
    Click External Link and Return      partial link:Frequently Asked Questions     Looking for more information about us?  Find out more from ...... Get your questions answered.

关键字是:

Click External Link and Return
    [Arguments]     ${Element}  ${ExtUrl}
    ${CurrTitle}=   get title
    click element   ${Element}
    sleep   5s
    run keyword and continue on failure  select window  ${ExtUrl}
    sleep   1s
    run keyword and continue on failure  select window   ${CurrTitle}

有没有一种方法可以使用标题中的部分文本来定位和导航到窗口?请提出在机器人框架,Python或JavaScript中是否有任何方法可以实现这一目标。

考特斯特拉

使用下面的示例,custom关键字将使用当前的浏览器,然后使用模式搜索选项卡并选择它。如果结果多于或少于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]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档