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.

3
  • Is the gallery publicly accessible? Commented May 4, 2019 at 10:14
  • @QHarr - Sorry it's not publicly accessible Commented May 4, 2019 at 10:28
  • Can you post few lines of code how you are doing it.then OP might found what’s wrong in your code block? Commented May 4, 2019 at 12:07

1 Answer 1

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')) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.