gpt4 book ai didi

selenium-webdriver - 如何从 WebDriver 的本地缓存中保存图像?

转载 作者:行者123 更新时间:2023-12-04 10:01:41 30 4
gpt4 key购买 nike

我通过 WebDriver (Chrome) 从网页下载图像

// STEP 1
$driver->get($link);

// STEP 2
$els=$driver->findElements(WebDriver\WebDriverBy::tagName('img'));

foreach ($els as $el) {
$src=$el->getAttribute('src');
$image=file_get_contents($src);
file_put_contents("image.jpg",$image);
}

虽然浏览器已经加载了图像,但我需要在步骤 2 中再次下载图像。

我可以通过 right-click 在第 1 步之后保存图像在浏览器中和 Save image as ...没有互联网连接,因为图像在浏览器的本地缓存中可用。

是否可以使用 WebDriver 保存 Chrome 加载的图像而无需再次下载?

上面的代码是 PHP ,但其他编程语言中的任何命中或示例代码都可以解决问题。

最佳答案

下面的 java 代码将在您想要的目录中下载图像(或任何文件)。

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class imageDownload{

public static void main(String[] args) throws IOException {
URL img_url = new URL("image URL here");
String fileName = img_url.getFile();
String destName = "C:/DOWNLOAD/DIRECTORY" + fileName.substring(fileName.lastIndexOf("/"));

InputStream is = img_url.openStream();
OutputStream os = new FileOutputStream(destName);

byte[] b = new byte[2048];
int length;

while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}

is.close();
os.close();
}
}

首先,图像的所有字节流将存储在对象“is”中,并且该字节流将被重定向到 OutputStream 对象 os 以创建文件(一种复制粘贴,但作为 0 和 1)。

关于selenium-webdriver - 如何从 WebDriver 的本地缓存中保存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61786337/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com