簡體   English   中英

如何在使用 selenium 截屏之前等待元素滿載

[英]How to wait the element full load before screenshot it with selenium

我有一個代碼,我用來從谷歌下載幾個圖像,然后我通過它的源下載它,但它花了很多時間,所以我開始使用 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()

可能換個思路,獲取圖片url,直接用下載工具下載圖片。 省去很多麻煩,參見示例: 使用 selenium python 下載圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM