skip to Main Content

This product have total five image. I am trying to get those five image links but getting only first image link. here is my code:

images = driver.find_elements_by_css_selector('#shopify-section-product-template .Image--lazyLoaded')

for image in images:
    image = image.get_attribute('data-original-src') 

#data-original-src contains link of each image

3

Answers


  1. That page is initially loaded with 1 image only.
    To get more images you have to scroll the page.
    So without scrolling the page, getting the images with

    images = driver.find_elements_by_css_selector('#shopify-section-product-template .Image--lazyLoaded')
    

    will have only 1 element in images list.
    UPD
    You can click on the dot’s navigation on the left side and then get the image:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 20)
    
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".Product__SlideshowNavScroller")))
    time.sleep(0.2)
    dots = driver.find_elements_by_css_selector('.Product__SlideshowNavScroller a')
    image_src = set()
    for dot in dots:
        dot.click()
        time.sleep(1)
        images = driver.find_elements_by_css_selector('#shopify-section-product-template .Image--lazyLoaded')
        for image in images:
            image_src.add(image.get_attribute('data-original-src'))
    

    image_src will finally contain all the images src links

    Login or Signup to reply.
  2. What is most likely happening here, is that the images are not rendered. To fix that, simply add a code snippet that will scroll to the bottom of the page, before the "find_element" function gets called.

    driver.execute_script("window.scrollTo(0, 990000)") 
    
    Login or Signup to reply.
  3. Your are getting them all because their classes are different.
    you will get first image with this:

    images = driver.find_elements_by_css_selector('#shopify-section-product-template .Image--lazyLoaded')
    

    but to get other four use : .Image–fadeIn class

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search