python - 在 Robot Framework 或 Python 中使用部分 url 值选择窗口

标签 python selenium-webdriver robotframework

我需要通过单击网页上的元素来测试外部链接是否有效且可访问。下面是一个示例,我们需要单击链接导航到 panynj.gov。问题是:

  1. 在html代码中,url编码为href="http://www.panynj.gov/",因此缺少安全协议(protocol)https:
  2. 如果我们点击该链接,打开的 URL 为“https://www.panynj.gov/port-authority/en/index.html

由于新打开的 URL 与原始 href 值不匹配,因此我的脚本无法选择新打开的选项卡。

我正在寻找一种解决方案,可以:

  1. 使用部分 URL 文本选择新打开的选项卡。例如,我可以从 href 值中删除 http://并使用“www.panynj.gov”来选择新打开的选项卡,或者
  2. 我可以从所有选项卡中选择最后打开的选项卡(从技术上讲,该选项卡应该是在元素上单击操作后打开的选项卡)并运行检查。

我的代码如下:

*** Test Cases ***

Test for Selecting Tab using partial URL
   go to                ${HomePage}
   Click Element        xpath://*[@href="http://www.panynj.gov/"]   
   Test Navigation to unsecured url     http://www.panynj.gov/      ${HomepageTitle}


*** Keywords ***

Test Navigation to unsecured url
    [Arguments]     ${href}     ${Title_}
    ${str} =   Replace String   ${href}    http    https
    select window    url:${str}
    ${location}     get location
    run keyword if  "${Location}"=="${str}"    close window
    select window    ${Title_}

最佳答案

关于python - 在 Robot Framework 或 Python 中使用部分 url 值选择窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60084037/

相关文章:

java - 如何使用 Selenium (Java) 禁用浏览器中的 JavaScript?

java - 如何使用 Selenium WebDriver 选择 div id?

java - 有没有办法在远程主机上运行 Selenium 测试?

robotframework - 将 header 与 Robot Framework 请求库的 'Get Request' 关键字一起使用

python - 使用嵌入式 python 和 SimpleTemplate Engine 作为字符串传递给 template()

python - 是否有用 Python 编写的 GEDCOM 解析器?

python - asyncio 客户端服务器在 docker 中不起作用

python - 使用 Robot Framework API 在 Python 中创建测试用例

java - 如何在 Java 中模拟随机机器人按键?

javascript - 在 Flask 应用上获取日期时间数据时出错