title: Navigating to a new web page and accessing its contents
body: <p>I want to navigate to a new page from a web page and access its contents and then again return back to the original page and access the contents of the original page. But it throws an error. </p>
  
  <p>I am able to navigate to a new page and access its contents and also I am able to navigate to the original page but unable to access contents of the original page.</p>
  
  <p>Here is the <a href="https://www.amazon.com/s/ref=sr_pg_1?sort=salesrank&amp;ie=UTF8&amp;rh=n%3A133140011&amp;page=1&amp;unfiltered=1" rel="nofollow noreferrer">webpage</a> I want to parse.</p>
  
  <p>Here is the code which i have been working on :-</p>
  
  <pre><code>import sys
  import csv
  import os
  import time
  import urllib
  
  from selenium import webdriver
  from selenium.webdriver.common.keys import Keys
  from selenium.webdriver.common.action_chains import ActionChains
  
  url = 'https://www.amazon.com/s/ref=sr_pg_1?sort=salesrank&amp;ie=UTF8&amp;rh=n%3A133140011&amp;page=1&amp;unfiltered=1'
  driver = webdriver.Chrome();
  driver.maximize_window() #For maximizing window
  driver.get(url);
  driver.implicitly_wait(3) #gives an implicit wait for 3 seconds
  while driver.execute_script("return document.readyState") != 'complete':
      pass;
  elem =driver.find_elements_by_xpath("/html/body/div[1]/div[2]/div/div[3]/div[2]/div/div[4]/div[1]/div/ul/li");
  for i,item in enumerate(elem):
      temp = 0; #temp =0 refers it is best seller;
      try:
          if item.find_element_by_xpath("div/div[1]/div/a/span[1]/span").text.encode('utf-8') == "Best Seller":
              print "Best Seller :- Yes";
      except:
          print "Best Seller :- No"
          temp = 1;
      if temp == 0:
  
          print "Name of Book :- %s" %(item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[1]/a/h2").text.encode('utf-8'));
          #enter.write("Name of Book :- %s\n" %(item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[1]/a/h2").text.encode('utf-8')))
  
          print "Release Date :- %s" %(item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[1]/span[3]").text.encode('utf-8'));
          #enter.write("Release Date :- %s\n" %(item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[1]/span[3]").text.encode('utf-8')));
  
          print "Author :- %s" %(item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[2]/span[2]").text.encode('utf-8'));
          #enter.write("Author :- %s\n" %(item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[2]/span[2]").text.encode('utf-8')));
  
          print "URL-Page of the book :- %s" %(item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[1]/a").get_attribute("href"));
          #enter.write("URL-Page of the book :- %s\n" %(item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[1]/a").get_attribute("href")));          
          new_url = item.find_element_by_xpath("div/div[2]/div/div[2]/div[1]/div[1]/a").get_attribute("href") 
  
          driver.get(new_url);
          while driver.execute_script("return document.readyState") != 'complete':
              pass;
          for nums in range(1,10):    
              if driver.find_element_by_xpath("""//*[@id="productDetailsTable"]/tbody/tr/td/div/ul/li[%s]/b"""%(nums)).text == 'ASIN:':
                  print driver.find_element_by_xpath("""//*[@id="productDetailsTable"]/tbody/tr/td/div/ul/li[%s]"""%(nums)).text.encode('utf-8');
                  break;
          print "No. of 5 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[1]/td[3]/a""").text.encode('utf-8'));
          print "No. of 4 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[2]/td[3]/a""").text.encode('utf-8'));   
          print "No. of 3 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[3]/td[3]/a""").text.encode('utf-8'));
          print "No. of 2 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[4]/td[3]/a""").text.encode('utf-8'));
          print "No. of 1 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[5]/td[3]/a""").text.encode('utf-8'));
          driver.back();
      else:
          flag = item.find_element_by_xpath("div/div/div/div[2]")
          print "Name of Book :- %s" %(flag.find_element_by_xpath("div[1]/div[1]/a/h2").text.encode('utf-8'));
          print "Release Date :- %s" %(flag.find_element_by_xpath("div[1]/div[1]/span[3]").text.encode('utf-8'));
          print "Author :- %s" %(flag.find_element_by_xpath("div[1]/div[2]/span[2]").text.encode('utf-8'));
          print "URL-Page of the book :- %s" %(flag.find_element_by_xpath("div[1]/div[1]/a").get_attribute("href"));          new_url = flag.find_element_by_xpath("div[1]/div[1]/a").get_attribute("href") 
  
          driver.get(new_url);
          while driver.execute_script("return document.readyState") != 'complete':
              pass;
          for nums in range(1,10):    
              if driver.find_element_by_xpath("""//*[@id="productDetailsTable"]/tbody/tr/td/div/ul/li[%s]/b"""%(nums)).text.encode('utf-8') == 'ASIN:':
                  print driver.find_element_by_xpath("""//*[@id="productDetailsTable"]/tbody/tr/td/div/ul/li[%s]"""%(nums)).text.encode('utf-8');
                  break;
          print "No. of 5 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[1]/td[3]/a""").text.encode('utf-8'));
          print "No. of 4 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[2]/td[3]/a""").text.encode('utf-8'));   
          print "No. of 3 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[3]/td[3]/a""").text.encode('utf-8'));
          print "No. of 2 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[4]/td[3]/a""").text.encode('utf-8'));
          print "No. of 1 star ratings :- %s"%(driver.find_element_by_xpath("""//*[@id="histogramTable"]/tbody/tr[5]/td[3]/a""").text.encode('utf-8'));       
  
          driver.back();
  </code></pre>
  
  <p>It gives me the following error </p>
  
  <blockquote>
    <p>at line 57 i.e.</p>
    
    <p>flag = item.find_element_by_xpath("div/div/div/div[2]") selenium.common.exceptions.StaleElementReferenceException: Message:
    stale element reference: element is not attached to the page document</p>
  </blockquote>
  
  <p>Can anybody help me on this ?</p>