title: Loop through links and scrape data from resulting pages using Selenium + Python
body: <p>I am new to Selenium and need to scrape a website that contains a list of links structured exactly like:</p>
  
  <pre><code>&lt;a class="unique" href="..."&gt;
      &lt;i class="something"&gt;&lt;/i&gt;
      "Text - "
      &lt;span class="something"&gt;Text&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class="unique" href="..."&gt;
      &lt;i class="something"&gt;&lt;/i&gt;
      "Text - "
      &lt;span class="something"&gt;Text&lt;/span&gt;
  &lt;/a&gt;
  ...
  ...
  </code></pre>
  
  <p>I need to click on this list of links inside a loop and scrape data from result pages.
  What I have done up till now is:</p>
  
  <pre><code>lists = browser.find_elements_by_xpath("//a[@class='unique']")
  for lis in lists:
      print(lis.text)
      lis.click()
      time.sleep(4)
      # Scrape data from this page (works fine).
      browser.back()
      time.sleep(4)
  </code></pre>
  
  <p>It works fine for the first loop but when the second loop reaches</p>
  
  <pre><code>print(lis.text)
  </code></pre>
  
  <p>It throws an error saying:</p>
  
  <blockquote>
    <p>StaleElementReferenceException: Message: stale element reference: element is not attached to the page document</p>
  </blockquote>
  
  <p>I have tried <code>print (lists)</code> and it gives the list of all the link elements so works fine. The problem occurs when the browser comes back to the previous page. I have tried extending time and using <code>browser.get(...)</code> instead of <code>browser.back()</code> but the error still remains. I don't get why it will not print <code>lis.text</code> because lists still contain a list of all the elements. Any help would be greatly appreciated.</p>