How to navigate to new browser window using partial title text using either Python, or JavaScript or Robot Framework and Selenium(如何使用Python、JavaScript或Robot Framework和Selenium使用部分标题文本导航到新浏览器窗口)
问题描述
在我的测试期间,我们需要单击在新选项卡中打开网页的链接。在某些情况下,我们看到测试由于标题文本中包含多个空格或制表符而失败。这类标题的一个示例是-
"Looking for more information about US?${SPACE}${SPACE}Find out more from ... Get your questions answered."
我尝试使用部分文本"Looking for more information about US"而不是使用整个标题文本切换到窗口。但到目前为止还没有找到这样做的方法。
我当前的代码如下:
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来实现这一点。
推荐答案
使用下面的示例,自定义关键字将使用当前浏览器,然后使用模式搜索选项卡并选择它。如果结果多于或少于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]
这篇关于如何使用Python、JavaScript或Robot Framework和Selenium使用部分标题文本导航到新浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用Python、JavaScript或Robot Framework和Selenium使用部分标题文本导航到新浏览器窗口
基础教程推荐
- 最佳动态 JavaScript/JQuery 网格 2022-01-01
- CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符 2022-01-01
- 逻辑运算符 ||在 javascript 中,0 代表 Boolean false? 2022-01-01
- 使用 jQuery 在悬停时交换 DIV 类 2022-01-01
- 从快速中间件中排除路由 2022-01-01
- 当木偶师打开Chrome时,不能使用Chrome扩展 2022-01-01
- 即使每次插入第一个输入的值不同,第二个输入仍显示相同的输入值 2022-01-01
- HTML5 画布调整为父级 2022-01-01
- 在 Javascript 中使用 Fetch API 上传文件并显示进度 2022-01-01
- 带角度的选项卡:仅使用 $http 在单击时加载选项卡 2022-01-01
