繁体   English   中英

Robot Framework Selenium For loop to click on nav links fails with a StaleElementReferenceException

[英]Robot Framework Selenium For loop to click on nav links fails with a StaleElementReferenceException

我有这个测试应该获取导航菜单中的所有链接并单击它们以确保它们工作...但是,测试失败,因为页面重新加载并且我收到此错误消息

StaleElementReferenceException:消息:元素引用已过时; 元素不再附加到 DOM,它不在当前框架上下文中,或者文档已被刷新

我相信这个错误是因为当按下链接时页面正在重新加载,并且正在刷新 dom,这导致它失败,因为在 log.html 中循环的第一次迭代成功,但其他人没有。

我想在不对链接进行硬编码的情况下实现这一点,因为我想在多个页面上重复使用这个测试。

任何帮助将不胜感激,无论如何谢谢!

这是代码...

*** Settings ***
Library     SeleniumLibrary

Test Setup    Open Browser    ${URL}    ${BROWSER}
Test Teardown    Close Browser

*** Variables ***
${BROWSER}      Firefox
${URL}          https://coupa.com/blog

*** Test Cases ***
# Validate Homepage Title
#     Title Should Be     Home | Coupa Cloud Platform for Business Spend | Travel and Expense Management, Procurement, and Invoicing

Validate MenuLinks
    Validate MenuLinks

*** Keywords ***
Validate MenuLinks
    ${links} =  Get WebElements  //*[@id="block-system-menu-block-blog-term-menu"]/div/div/ul/li/a

    FOR  ${link}  IN  @{links}
        Maximize Browser Window  
        Wait Until Page Contains Element  ${link}
        Click Link  ${link}
        Go To  ${URL}
    END

我为任何对如何执行此操作感到好奇的人解决了这个问题

问题出在 for 循环中

这是修复它的代码

${links} =  SeleniumLibrary.Get Element Count  ${ul_location}/li/a

FOR  ${index}  IN RANGE  ${links}
    ${new_link} =  Get WebElement  ${ul_location}/li[${index} + 1]/a
    Wait Until Page Contains Element  ${new_link}
    Mouse Over  ${new_link}
    Click Link  ${new_link}
    ${title} =  Get Title
    Should Not Be Equal  ${title}  Page not found | Coupa Cloud Platform for Business Spend | Travel and Expense Management, Procurement, and Invoicing   msg='Navlinks should not go to 404 URL'
END

我认为它失败了,因为它试图访问@{links} 列表中的项目,但它们被分配了唯一标识符,该标识符仅在浏览器 window 打开时存在,因此当浏览器刷新时,它会覆盖 ids 和打破测试...

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM