title: get StaleElementReferenceException error while using driver.navigate().back() in a loop in selenium
body: <p>I have the following code, which gets a list of elements and then loops through it while using <code>driver.navigate().back();</code></p>
  
  <pre><code>List&lt;WebElement&gt; listingWebElementList = driver.findElements(By.xpath("(//span[@id='titletextonly'])"));
  
  for (WebElement listingElement : listingWebElementList)
  {
      Thread.sleep(5000);
      listingElement.click();
      Thread.sleep(5000);
      driver.navigate().back();
  }
  </code></pre>
  
  <p>On the second round of the loop I get the following error when using the chromedriver</p>
  
  <blockquote>
    <p>org.openqa.selenium.StaleElementReferenceException: stale element
    reference: element is not attached to the page document</p>
  </blockquote>
  
  <p>and I get the following error with the FirefoxDriver</p>
  
  <blockquote>
    <p>org.openqa.selenium.StaleElementReferenceException: Element not found
    in the cache - perhaps the page has changed since it was looked up</p>
  </blockquote>
  
  <p>Can the <code>driver.navigate().back();</code> not be used inside a loop as above?</p>