简体   繁体   English

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

[英]How to navigate to new browser window using partial title text using either Python, or JavaScript or Robot Framework and Selenium

During my tests, we need to click on links that open webpages in new tab.在我的测试中,我们需要点击在新标签页中打开网页的链接。 In some cases, we see the test fail due to inclusion of multiple space or tab characters in the title text.在某些情况下,我们会看到由于标题文本中包含多个空格或制表符而导致测试失败。 One of the examples of such titles are -此类标题的示例之一是-

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

I am trying to switch to the window using the partial text "Looking for more information about US" instead of using the whole title text.我正在尝试使用部分文本“寻找有关美国的更多信息”而不是使用整个标题文本切换到窗口。 But so far could not find a way to do that.但到目前为止还没有找到一种方法来做到这一点。

My current codes are as follows:我目前的代码如下:

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.

and the keyword is :关键字是:

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}

Is there a way to locate and navigate to the window using partial text from the title?有没有办法使用标题中的部分文本来定位和导航到窗口? Kindly suggest if there is any way in robot framework, Python or JavaScript to achieve this.请建议机器人框架、Python 或 JavaScript 中是否有任何方法可以实现这一点。

Using the below example the custom keyword will use the current browser and then using a pattern search for the tab and select it.使用以下示例,自定义关键字将使用当前浏览器,然后使用模式搜索选项卡并选择它。 If there are more or less than 1 result, it will fail.如果结果多于或少于 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在硒机器人框架脚本中使用JavaScript单击元素? - How to click an element using javascript in selenium robot framework script? 如何在硒机器人框架中使用JavaScript接受警报 - How to accept alert using javascript in selenium robot framework 如何使用JavaScript打开新的浏览器窗口? - How to open new browser window using javascript? 如何使用 Python Selenium 导航由 javascript 触发的下拉菜单 - How to navigate a dropdown triggered by javascript using Python Selenium 使用 Selenium Python 获取浏览器 window 大小 - Get browser window size using Selenium Python 如何使用 selenium 机器人框架在 Firefox 中处理打印预览 - How to handle print preview in Firefox using selenium robot framework 如何在 Robot Framework 中使用 Execute Javascript new Date().toLocaleDateString() 将 output 存储到变量中? - How to store output to a variable using Execute Javascript new Date().toLocaleDateString() in Robot Framework? 如何打开新的浏览器窗口(不是选项卡),然后使用jQuery / Javascript调整其大小 - How to open new browser window (not tab) and then resize it using jQuery / Javascript 如何使用Selenium从JavaScript窗口获取文本? - How to get a text from a JavaScript window using Selenium? 使用JavaScript打开新的https浏览器窗口 - Open new https browser window using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM