如何使用 Selenium(任何版本)下载图像? [英] How to download an image using Selenium (any version)?

查看:40
本文介绍了如何使用 Selenium(任何版本)下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如何使用 selenium/webdriver 下载页面的图像.假设需要用户会话来下载图像,因此使用纯 URL 没有帮助.任何示例代码都受到高度赞赏.

I was wondering, how can one use selenium/webdriver to download an image for a page. Assuming that the user session is required to download the image hence having pure URL is not helpful. Any sample code is highly appreciated.

推荐答案

我更喜欢这样做:

1. Get the SRC attribute of the image.
2. Use ImageIO.read to read the image onto a BufferedImage
3. Save the BufferedImage using ImageIO.write function

例如

String src = imgElement.getAttribute('src');
BufferedImage bufferedImage = ImageIO.read(new URL(src));
File outputfile = new File("saved.png");
ImageIO.write(bufferedImage, "png", outputfile);

这篇关于如何使用 Selenium(任何版本)下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆