Ways to avoid "StaleElementReferenceException" errors in Selenium
P粉920835423
2023-08-21 17:47:18
<p>I'm implementing a lot of Selenium tests in Java - sometimes, my tests fail due to <code>StaleElementReferenceException</code>. </p>
<p>Can you suggest some ways to make the tests more stable? </p>
I once had this problem, but unbeknownst to me, BackboneJS was running on the page and it replaced the element I was trying to click on. My code is as follows.
This is of course functionally the same as the code below.
What occasionally happens is that between the find and the click, javascript replaces the checkoutLink element, ie.
This results in a StaleElementReferenceException exception when trying to click on the link. I couldn't find any reliable way to tell WebDriver to wait for the javascript to finish running, so this is how I ended up solving it.
This code will continue to try to click the link, ignoring the StaleElementReferenceException exception, until the click is successful or the timeout is reached. I like this solution because it takes away the hassle of writing retry logic and only uses WebDriver's built-in constructs.
This can occur if DOM operations taking place on the page temporarily render the element inaccessible. To deal with these situations, you can try to access the element multiple times in a loop until finally an exception is thrown.
Try using this excellent solution from darrelgrainger.blogspot.com: