How to fix StaleElementReferenceExceptions
Running your selenium tests you might be getting StaleElementReferenceExceptions , that means your tests are poorly written and you should look for a change.
Selenium::WebDriver::Error::StaleElementReferenceError: stale element reference: element is not attached to the page document
When is StaleElementReferenceExceptions raised ?
The reason for this is because the element to which you have referred is removed from the DOM structure
WebElement element = driver.findElement(By.id("foo")); // DOM changes - page is refreshed, or element is removed and re-added element.click();
Now at the point where you’re clicking the element, the element reference is no longer valid.
Fix StaleElementReferenceExceptions in ruby Capybara
Capybara 2 introduces a new method called
synchronize. While this method is part of the public API, don’t run off and use it just yet. It has a very distinct use case, which is completely different from what you’re probably using
wait_until for. You will most likely only ever have to call this if you access the low level driver directly through
#native. In this case, you might receive errors like Selenium’s
StaleElementReferenceError. It’s these kinds of errors that
jnicklas commented on Feb 5 2013
Unless you have a very good understanding of how the Capybara codebase works, I would recommend not to use
synchronizeat all, it’s a very tricky method. If you really must use a waiting function, you can find one in the linked blog post, but I suggest you try to understand the point it makes and avoid using it in the first place, there are very few cases where you need to wait with Capybara.
Since the release Capybara 2.0.0 removed the
wait_until method from the API. This seems to have frustrated a few people, so let me explain why this decision was reached and what your options are for the future.
Wait until can be implemented like this :
Usage of wait until: