简体   繁体   English

如何在使用 selenium 截屏之前等待元素满载

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

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.我有一个代码,我用来从谷歌下载几个图像,然后我通过它的源下载它,但它花了很多时间,所以我开始使用 selenium .screenshot_as_base64 因为我的代码已经有一个系统来保存 base64进入图像,问题是,一段时间后,代码运行速度比图像加载快,所以代码开始保存空白图像截图,所以我需要知道是否有办法在元素完全加载之前等待屏幕截图。

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

Maybe think differenly, get the image url and download the image directly using a download tool.可能换个思路,获取图片url,直接用下载工具下载图片。 Saves a lot of hassle, see for eample: Download image with selenium python省去很多麻烦,参见示例: 使用 selenium python 下载图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python3中使用Selenium和phantomjs等待网页截图之前? - How to wait before screenshot of webpage using selenium and phantomjs in python3? 提交表单后,如何在点击所述元素之前等待元素加载? (Selenium / Python) - After submitting form, how to wait for element to load before clicking on said element? (Selenium / Python) 让硒等待,然后在python中截屏 - Make selenium wait before taking a screenshot in python Selenium 如何截取元素截图? - Selenium how to capture element screenshot? 使 Selenium 不等到页面完全加载 - Makes Selenium not wait till full load of page 如何使用 Selenium 和 Screenshot 在 Python 中获取整页截图 - How to get a full-page screenshot in Python using Selenium and Screenshot 如何让脚本等待元素加载 selenium - How would I make a script wait for a element to load with selenium 在使用python和selenium单击之前,如何使用等待预期条件等待元素中的属性包含特定值 - How to use wait expected condition to wait for the attribute in an element to contain certain value before clicking using python and selenium Selenium Python - 等待元素加载并点击它 - Selenium Python - Wait for Element to load and click on it Selenium 等待加载页面上不存在的元素 - Selenium wait for an element not present on load page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM