0

I am trying to go through each products in my catalogue and print product image links. Following is my code.

product_links = driver.find_elements_by_css_selector(".product-link")
for link in product_links:
    driver.get(link.get_attribute("href"))
    images = driver.find_elements_by_css_selector("#gallery img")
    for image in images:
        print(image.get_attribute("src"))
    driver.back()

But I receiving the error selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document, I think this is happening because when we go back to catalogue page the page get loaded again and the element references in product_links became stale.

How we can avoid this issue? is there any better solution for this?

Unnikrishnan
  • 2,683
  • 5
  • 22
  • 39
  • The simplest solution is to [refresh](https://selenium-python.readthedocs.io/api.html?highlight=refresh#selenium.webdriver.remote.webdriver.WebDriver.refresh) the page to re-establish the dom objects – G. Anderson May 03 '19 at 16:36
  • @G.Anderson - Still same issue – Unnikrishnan May 03 '19 at 16:45
  • Duplicate of https://stackoverflow.com/a/16244739/677518 – Ardesco May 04 '19 at 07:42
  • Possible duplicate of [Selenium WebDriver How to Resolve Stale Element Reference Exception?](https://stackoverflow.com/questions/16166261/selenium-webdriver-how-to-resolve-stale-element-reference-exception) – Ardesco May 04 '19 at 07:42

1 Answers1

0

I ran into a similar problem, and here's how I solved it. Basically, you have to refresh the page and re-establish the list of links each time you return to the page.Of course, doing this you can't use a for loop, because your objects are stale each time.

Unfortunately I can't test this, as I don't have access to your actual URL, but this should be close

def get_prod_page(link):
    driver.get(link.get_attribute("href"))
    images = driver.find_elements_by_css_selector("#gallery img")
    for image in images:
        print(image.get_attribute("src"))
    driver.back()

counter=0
link_count= len(driver.find_elements_by_css_selector(".product-link"))
while counter <= link_count:
    product_links = driver.find_elements_by_css_selector(".product-link")[counter:]
    get_prod_page(product_links[0])
    counter+=1
    driver.refresh()
G. Anderson
  • 5,815
  • 2
  • 14
  • 21