[英]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.