How to fix StaleElementReferenceExceptions


643
643 points
StaleElementReferenceExceptions
StaleElementReferenceExceptions

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.


WebDriverWait wait = new WebDriverWait(driver, 10);

// while the following loop runs, the DOM changes - 
// page is refreshed, or element is removed and re-added
wait.until(presenceOfElementLocated(By.id("container-element")));        

// now we're good - let's click the element
driver.findElement(By.id("foo")).click();
//The presenceOfElementLocated() method would look something like this:

private static Function<WebDriver,WebElement> presenceOfElementLocated(final By locator) {
    return new Function<WebDriver, WebElement>() {
        @Override
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    };
}
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 synchronize prevents.

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 synchronize at 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 :
def wait_until
  require "timeout"
  Timeout.timeout(Capybara.default_wait_time) do
    sleep(0.1) until value = yield
    value
  end
end
Usage of wait until:
wait_until { page.should have_content("something") }

P.S If your tests ever raised no such session error here is how to fix that too!

Happy testing!


Like it? Share with your friends!

643
643 points
Test engineer

0 Comments

Your email address will not be published. Required fields are marked *

2 × 2 =