Robot Framework Selenium For 循环点击导航链接失败,出现 StaleElementReferenceException

时间:2021-01-13 23:08:23

标签: python selenium robotframework selenium2library

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

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

1 个答案:

答案 0 :(得分:1)

我为任何好奇的人解决了这个问题

问题出在 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} 列表中的项目,但它们被分配了唯一标识符,只有在浏览器窗口打开时才存在,因此当浏览器刷新时,它会覆盖ids 并打破测试...

相关问题