0

I have a code that i use to download several images from google, before i was downloading it by it's source, but it was taking a lot of time, so i started to using selenium .screenshot_as_base64 since my code already has a system to save base64 into images, the problem is, after some time, the code runs faster than the images load, so the code starts to save blank images screenshots, so i need to know if there is a way to wait before the element is fully loaded before take the screenshot.

here is my code to help:

def search(self,keyword,n):

    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="sbtc"]/div/div[2]/input')))
    actionChains = ActionChains(self.driver)
    actionChains.move_to_element(element).click().perform()
    actionChains.move_to_element(element).send_keys(keyword,Keys.RETURN).perform()
    element = self.wait.until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="islrg"]/div[1]/div[1]/a[1]/div[1]/img')))
    actionChains.move_to_element(element).click().perform()
    for i in range(n):
        element = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="Sva75c"]/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div[2]/div/a/img')))
        src = element.screenshot_as_base64
        self.download_base(src, keyword)
        print(src)
        element = self.wait.until(
            EC.presence_of_element_located(
                (By.XPATH, '//*[@id="Sva75c"]/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div[1]/a[3]')))
        actionChains.move_to_element(element).click().perform()
    self.index = 0
    self.driver.quit()

1 Answers1

0

Maybe think differenly, get the image url and download the image directly using a download tool. Saves a lot of hassle, see for eample: Download image with selenium python

Taco Verhagen
  • 222
  • 1
  • 6