pythonseleniumrobotframeworkselenium2library

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


I have this test which is supposed to get all the links in the nav menu and click them to make sure they work... However, the test is failing because the page reloads and i get this error message

StaleElementReferenceException: Message: The element reference of is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed

I believe the error is because the page is reloading when the link is pressed, and is refreshing the dom, which is causing it to fail, because in the log.html the first iteration of the loop succeeds, but the others do not.

I want to achieve this without hardcoding the links because I want to reuse this test across several pages.

any help would greatly be appreciated, anyway thanks!

Here is the code...

*** 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

Solution

  • I fixed the problem for anyone curious about how to do this

    the problem was in the for loop

    here is the code that fixed it

    ${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
    

    I believe that it was failing because it was trying to access the items in the @{links} list, but they are assigned unique identifiers that only exist when the browser window is open, thus when the browser is refreshed, it overrides the ids and breaks the test...