【问题标题】:Robot Framework Selenium For loop to click on nav links fails with a StaleElementReferenceExceptionRobot Framework Selenium For 循环单击导航链接失败并出现 StaleElementReferenceException
【发布时间】:2021-04-19 00:10:43
【问题描述】:

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

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

【问题讨论】:

    标签: python selenium robotframework selenium2library


    【解决方案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 并打破了测试...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      • 2021-10-26
      • 2015-09-12
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      相关资源
      最近更新 更多