How to download all photos?

Question:

Why is my code downloading only 16 images?

enter link description here

enter image description here

driver.get(url)
driver.implicitly_wait(50)
for x in range(7):
    driver.execute_script("window.scrollBy(0, 200)")
    sleep(0.5)

driver.find_element(By.CLASS_NAME, "facetwp-load-more").click()


counter = 1
for i in     driver.find_elements(By.XPATH,"/html/body/div[6]/div[2]/div/div/div/div/div/div[1]/a/div/img"): 
    atributoSrc = i.get_attribute("src")
    file_name = f"image{counter:02d}.jpg"
    imagem.append(atributoSrc)
    urllib.request.urlretrieve(atributoSrc,f"C:\__Imagens e Planilhas     Python\Facebook\Imagens\ImagensJPG\{file_name}") 
    counter += 1  
Asked By: Felipe

||

Answers:

I created a https://github.com/seleniumbase/SeleniumBase script to download all those images as files:

import os
from seleniumbase import BaseCase

if __name__ == "__main__":
    from pytest import main
    main([__file__])

class MyTestClass(BaseCase):
    def test_base(self):
        self.open("https://focastock.com/?s=food")
        img_elements_with_src = self.find_elements("img[src]")
        unique_src_values = []
        for img in img_elements_with_src:
            src = img.get_attribute("src")
            if src not in unique_src_values:
                unique_src_values.append(src)
        print()
        for src in unique_src_values:
            if src.split(".")[-1] not in ["png", "jpg", "jpeg"]:
                continue
            self.download_file(src)  # Goes to downloaded_files/
            filename = src.split("/")[-1]
            self.assert_downloaded_file(filename)
            folder = "downloaded_files"
            file_path = os.path.join(folder, filename)
            self._print(file_path)

All the images were successfully downloaded.

Answered By: Michael Mintz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.