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?