Mozilla - 53.0 and Mozilla - 54.0b2 Gecko driver - 16.1 Selenium 3.4

org.openqa.selenium.StaleElementReferenceException: The element reference of  stale: either the element is no longer attached to the DOM or the page has been refreshed
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'

Happens for the first element after navigating from the homepage to a different page

closed as unclear what you're asking by FDM, Bharat Mane, IAmMilinPatel, NarendraC, ECiurleo May 9 '17 at 8:45

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can you share code. Because stale exception comes when you create the Element and after that your page got refresh. – saurabhguptacse May 2 '17 at 10:04
  • Need to see code/page/... – FDM May 2 '17 at 10:24

When navigating from one webpage to another you need to wait for the element's presence / visibility before performing any operations on it.

public WebElement find(WebDriver driver, By locator) {
    WebElement element = null;

    // Maximum number of retries
    int retry = 3;

    // Timeout for a single try
    int timeoutInSeconds = 3;

    do {
        // Initialize wait on the defined web driver
        WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);

        // Wait until the visibility of the web element based on locator

        // If element is present, then it findElement() will return size()>0
        if(driver.findElements(locator).size() > 0) {

            // Store the first instance of the web element
            // Similar to using driver.findElements(locator).get(0);
            element = driver.findElement(locator);

        // If element is found
        if(element!=null) {
            // exit do-while loop
        } else {
            // retry again
        // stop retrying if the element is found or there are no retries left
    } while(element!=null && retry>0);

    // return the element (if not found will return null)
    return element;
  • This will not help the problem, and i dont see why you would ever need to wait 3 seconds, 3 seconds, 3 seconds, instead of 9 seconds when waiting for element. All you do is simply return null instead of error there. – George May 2 '17 at 8:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.