1

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

  • 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
-1

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
        wait.until(ExpectedConditions.visibilityOfElementLocated(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
            break;
        } else {
            // retry again
            retry--;
        }
        // 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.