Robot Framework Selenium For 循环点击导航链接失败并出现 StaleElementReferenceException
Robot Framework Selenium For loop to click on nav links fails with a StaleElementReferenceException
我有这个测试应该在导航菜单中获取所有 links 并单击它们以确保它们工作...但是,测试失败,因为页面重新加载并且我得到此错误消息
StaleElementReferenceException: 消息:元素引用已过时;元素不再附加到 DOM,它不在当前框架上下文中,或者文档已被刷新
我认为错误是因为在按下 link 时页面正在重新加载,并且正在刷新 dom,这导致它失败,因为在 log.html循环的第一次迭代成功,但其他的没有。
我想在不对 link 进行硬编码的情况下实现此目的,因为我想在多个页面上重复使用此测试。
任何帮助将不胜感激,无论如何谢谢!
这是代码...
*** 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 打开时存在,因此当浏览器已刷新,它覆盖了 id 并破坏了测试...
我有这个测试应该在导航菜单中获取所有 links 并单击它们以确保它们工作...但是,测试失败,因为页面重新加载并且我得到此错误消息
StaleElementReferenceException: 消息:元素引用已过时;元素不再附加到 DOM,它不在当前框架上下文中,或者文档已被刷新
我认为错误是因为在按下 link 时页面正在重新加载,并且正在刷新 dom,这导致它失败,因为在 log.html循环的第一次迭代成功,但其他的没有。
我想在不对 link 进行硬编码的情况下实现此目的,因为我想在多个页面上重复使用此测试。
任何帮助将不胜感激,无论如何谢谢!
这是代码...
*** 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 打开时存在,因此当浏览器已刷新,它覆盖了 id 并破坏了测试...