python - Navigating to a new web page and accessing its contents -
i want navigate new page web page , access contents , again return original page , access contents of original page. throws error.
i able navigate new page , access contents , able navigate original page unable access contents of original page.
here webpage want parse.
here code have been working on :-
import sys import csv import os import time import urllib selenium import webdriver selenium.webdriver.common.keys import keys selenium.webdriver.common.action_chains import actionchains url = 'https://www.amazon.com/s/ref=sr_pg_1?sort=salesrank&ie=utf8&rh=n%3a133140011&page=1&unfiltered=1' driver = webdriver.chrome(); driver.maximize_window() #for maximizing window driver.get(url); driver.implicitly_wait(3) #gives implicit wait 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"); i,item in enumerate(elem): temp = 0; #temp =0 refers 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 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 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; 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 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; 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();
it gives me following error
at line 57 i.e.
flag = item.find_element_by_xpath("div/div/div/div[2]") selenium.common.exceptions.staleelementreferenceexception: message: stale element reference: element not attached page document
can me on ?
your problem driver.back();
execute this. every single element have object reference in code becomes stale , invalid. , need re-create object.
so should recreate loop in such way inside loop
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");
and loop should not be
for i,item in enumerate(elem):
it should numeric loop, inside loop indexed association
item = elem[i]
Comments
Post a Comment