How to fix 'stale element reference exception' while trying to pick date from date-picker? How to fix 'stale element reference exception' while trying to pick date from date-picker? selenium selenium

How to fix 'stale element reference exception' while trying to pick date from date-picker?


In your first code, after you clicked on the element, the DOM changed, as in the Date became "14", and since the both the for-loop were still iterating, hence it threw StaleElementReferenceException.

Similarly, in the second code, you did break the "inside for-loop" that was iterating the td elements, but you didn't break the "outside" one, that continued with iterating the tr elements, hence it threw StaleElementReferenceException yet again.

Resolution:- You should've come out of both the for-loops using break after the element was clicked, and hence averting the StaleElementReferenceException, in the process.

Below code shows how you could've broken out of both the for-loops without any exception:

    WebDriver d=new FirefoxDriver();    d.manage().window().maximize(); //Maximizing window    d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Implicit wait for 20 seconds    Actions a=new Actions(d);    String date="14";    int flag=0;    d.get("http://www.eyecon.ro/bootstrap-datepicker/");    d.findElement(By.cssSelector("div#dp3>span")).click();    List<WebElement> trs=d.findElements(By.cssSelector("div.datepicker-days>table>tbody>tr"));    for(WebElement tr:trs) {        List<WebElement> tds=tr.findElements(By.tagName("td"));        for(WebElement td:tds) {            if(date.equals(td.getText())) {                a.moveToElement(td).click().build().perform();                flag=1; // Set to 1 when the required element was found and clicked.                break; //To come out of the first for-loop            }        }        if(flag==1)            break; //Since the element was found, come out of the second for-loop.    }

NOTE:- I have added the code for maximizing windows and providing implicit wait too, which is actually advised when you are writing selenium scripts.


You should use WebDriverWait and ExpectedConditions to tackle staleness of an element. Below is modified block of your code which I tested and it works.

 driver.findElement(By.cssSelector("div#dp3>span")).click(); WebDriverWait wait = new WebDriverWait(driver, 30); List<WebElement> trs = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("div.datepicker-days>table>tbody>tr"))); datePicker: {    for (WebElement tr : trs) {       List<WebElement> tds = tr.findElements(By.tagName("td"));       for (WebElement td : tds) {                 wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(td)));                 if (date.equals(td.getText())) {                        td.click();                        break datePicker;                 }        }     } }

For more information check WebDriverWait and ExpectedConditions here


Found an easier way to resolve my Stale Element Reference Exceptions.

In Java with Selenium2 try the code below:

 public WebElement cleanAndRebuildElement(final WebElement ee) {        WebElement e2;        try {            e2=ee;            e2.isDisplayed();            logger.info("Element is cleaned : Not Stale Anymore !");        } catch (StaleElementReferenceException e) {              e2=null;                  } catch(NoSuchElementException nse) {nse.printstacktrace();           }        return e2;    }}