title: How to resolve, Stale element exception? if element is no longer attached to the DOM?
body: <p>I have a question regarding "Element is no longer attached to the DOM".</p>
  
  <p>I tried different solutions but they are working intermittent. Please suggest a solution that could be permanent.</p>
  
  <pre><code>WebElement getStaleElemById(String id, WebDriver driver) {
      try {
          return driver.findElement(By.id(id));
      } catch (StaleElementReferenceException e) {
          System.out.println("Attempting to recover from StaleElementReferenceException ...");
          return getStaleElemById(id, driver);
      }
  }
  
  WebElement getStaleElemByCss(String css, WebDriver driver) {
      try {
          return driver.findElement(By.cssSelector(css));
      } catch (StaleElementReferenceException e) {
          System.out.println("Attempting to recover from StaleElementReferenceException ...");
          return getStaleElemByCss(css, driver);
      } catch (NoSuchElementException ele) {
           System.out.println("Attempting to recover from NoSuchElementException ...");
           return getStaleElemByCss(css, driver);
      }
  }
  </code></pre>
  
  <p>Thanks,
  Anu</p>