title: How to fix 'stale element reference exception' while trying to pick date from date-picker?
body: <p>I am trying to select date from the <em>Datepicker</em>.Following is the code </p>
  
  <pre><code>WebDriver d=new FirefoxDriver();
  Actions a=new Actions(d);
  String date="14";
  d.get("http://www.eyecon.ro/bootstrap-datepicker/");
  d.findElement(By.cssSelector("div#dp3&gt;span")).click();
  List&lt;WebElement&gt; trs=d.findElements(By.cssSelector("div.datepicker-days&gt;table&gt;tbody&gt;tr"));
         for(WebElement tr:trs) {
              List&lt;WebElement&gt; tds=tr.findElements(By.tagName("td"));
              for(WebElement td:tds) {
                  if(date.equals(td.getText())) {
                      a.moveToElement(td).click().build().perform();
  
                  }
              }
  </code></pre>
  
  <p>}</p>
  
  <p>With the above code i got stale element reference exception at this line of code</p>
  
  <pre><code>"if(date.equals(td.getText())) {"
  </code></pre>
  
  <p>so i have changed the code to this</p>
  
  <pre><code>for(WebElement td:tds) {
                  while(i&lt;4) {
                      try {
                          if(date.equals(td.getText())) {
                              a.moveToElement(td).click().build().perform();
  
                          }
                          break;
                      }catch(Exception ex) {
  
                      }
                      System.out.println(i);
                      i++;
                  }
              }
  </code></pre>
  
  <p>Now i am able to select the date.But the script is still throwing the stale element reference exception.The script is showing error at this line now</p>
  
  <pre><code>List&lt;WebElement&gt; tds=tr.findElements(By.tagName("td"));
  </code></pre>
  
  <p>I am working on this for the past 3 days.Any suggestions on how to solve this.
  Thanks in advance</p>