Reputation: 19
after going to a page and returning back to the previous page with driver.get(), I get this error while finding an element from that page:
File "C:\Users\ASUS\Desktop\x\index4.py", line y, in <module>
basket = item.find_elements_by_xpath('xpath')
(...)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
my code:
list_url = "URL"
driver.get(list_url)
staleElement = True
while staleElement:
staleElement = False
driver.refresh()
list_items = driver.find_elements_by_class_name("classname1")
for item in list_items:
basket = False
try:
basket = item.find_elements_by_xpath('xpath')
except exceptions.StaleElementReferenceException as e:
basket = item.find_elements_by_xpath('xpath')
if basket[0] and "text1" in basket[0].text:
price = item.find_elements_by_xpath('xpath1')[0].text
item_link = item.find_element_by_class_name("classname2").get_attribute("href")
if int(price) < 101:
driver.get(item_link)
if len(driver.find_elements_by_xpath('xpath2')) > 0:
driver.get(list_url)
staleElement = True
else:
driver.find_element_by_xpath('xpath3').click()
Upvotes: 0
Views: 1592
Reputation: 142641
Selenium doesn't gives real objects but only reference to objects in browser's memory and when you load new url (driver.get(...)
or click()
) then it loads new data to browser's memory and references to objects on previous page are outadted. They are outdated even if you load the previous page again (because objects may be in different place in browser's memory.
You have to use two for
-loops.
If first for
-loop you have to get all "href"
(item_link
) and append to some list (instead of driver.get(item_link)
). And when you will have all "href"
then in second for
-loop you may use driver.get(item_link)
.
I can't test it but it could be something like this:
list_url = "URL"
staleElement = True
while staleElement:
staleElement = False
driver.get(list_url) # load page instead of refreshing because in next loop it may have different page in memory
#driver.refresh()
list_items = driver.find_elements_by_class_name("classname1")
# first for-loop: get all `hrefs` (as strings)
all_hrefs = [] # list for all strings with `"href"`
for item in list_items:
basket = False
try:
basket = item.find_elements_by_xpath('xpath')
except exceptions.StaleElementReferenceException as e:
basket = item.find_elements_by_xpath('xpath')
if basket[0] and "text1" in basket[0].text:
price = item.find_elements_by_xpath('xpath1')[0].text
item_link = item.find_element_by_class_name("classname2").get_attribute("href")
if int(price) < 101:
all_href.append(item_link) # add string `"href"` to list
# second for-loop: use all `hrefs`
for item_link in all_hrefs:
driver.get(item_link)
if len(driver.find_elements_by_xpath('xpath2')) > 0:
staleElement = True
#driver.get(list_url) # there is no need to go back to previous page
#else:
# driver.find_element_by_xpath('xpath3').click() # there is no need to go back to previous page
Upvotes: 1