【问题标题】:How to wait the element full load before screenshot it with selenium如何在使用 selenium 截屏之前等待元素完全加载
【发布时间】:2021-12-21 09:08:44
【问题描述】:

我有一个代码,用于从谷歌下载几张图片,在我通过它的源下载它之前,但这需要很多时间,所以我开始使用 selenium .screenshot_as_base64 因为我的代码已经有一个系统将base64保存到图像中,问题是,一段时间后,代码运行速度比图像加载速度快,所以代码开始保存空白图像截图,所以我需要知道是否有办法在元素完全之前等待在截屏之前加载。

这是我的帮助代码:

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()

【问题讨论】:

  • 你看到了什么错误,在哪一行?
  • 它实际上并没有返回错误,问题是图像开始下载空白

标签: python selenium selenium-webdriver base64


【解决方案1】:

也许换个思路,获取图片url,直接使用下载工具下载图片。节省了很多麻烦,请参见示例: Download image with selenium python

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 2013-08-31
    • 2018-11-01
    • 2020-08-29
    • 2017-09-18
    • 2015-08-31
    • 2014-10-03
    • 2021-07-09
    相关资源
    最近更新 更多