0

I'm trying to get image links from my website, it has around 40 images and I'm using driver.find_elements_by_css_selector to get all the images to a list.

When I loop through this list and print id of these images, for the first 15 images it's working fine everything after that it throwing StaleElementReferenceException

I'm using WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'image-list'))) for loading my image gallery.

How I can fix this StaleElementReferenceException

Thanks.

Unnikrishnan
  • 2,683
  • 5
  • 22
  • 39

1 Answers1

1

I believe the container holding the images loading them dynamically, so you have to use the below logic.

# load the images in the dynamic list container
driver.find_element_by_xpath("(//*[@id='image-list']//img)[last()]").location_once_scrolled_into_view
time.sleep(1)
driver.find_element_by_xpath("(//*[@id='image-list']//img)[last()]").location_once_scrolled_into_view
time.sleep(1)
#get number of images
images = driver.find_elements_by_xpath("//*[@id='image-list']//img")
# use the for loop
for imageNum in range(len(images)):
    # access the image here
    image = driver.find_element_by_xpath("(//*[@id='image-list']//img[)" + str(imageNum+1) + "]")
    # now you can use the image element (first scroll to element)
    image.location_once_scrolled_into_view
    # get the image link here
    print(image.get_attribute('src'))
supputuri
  • 13,644
  • 2
  • 21
  • 39