我尝试使用机械手类保存图像...但是无法正常工作。
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://agiletesters.com.br");
Robot rb =new Robot();
rb.mouseMove(270, 160);
rb.mousePress(InputEvent.BUTTON3_DOWN_MASK);
rb.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
rb.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(11000);
rb.keyRelease(KeyEvent.VK_DOWN);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
在此右键单击中有效,但未选择“将图像另存为...”选项。
我尝试了此操作,但下载图像未显示:-
try {
driver = new FirefoxDriver();
driver.get("http://agiletesters.com.br");
System.setProperty("http.agent", "Firefox");
WebElement logo = driver.findElement(By.xpath("//*[@id=\"header-menu\"]/div/div[1]/a/img"));
String logoSRC = logo.getAttribute("src");
URL imageURL = new URL(logoSRC);
BufferedImage saveImage = ImageIO.read(imageURL.openStream());
ImageIO.write(saveImage, "png", new File("logo-forum.png"));
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.close();
}
答案 0 :(得分:0)
我的期望是您的BufferedImage saveImage = ImageIO.read(imageURL.openStream());
由于缺少Cookie header而失败
下载文件的正确方法是:
driver.managet().getCookies()
function 示例代码:
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder().url(logoSRC).addHeader("Cookie", cookies.toString()).build();
Response response = client.newCall(request).execute();
File downloadedLogo = new File("logo-forum.png");
BufferedSink sink = Okio.buffer(Okio.sink(downloadedLogo));
sink.writeAll(Objects.requireNonNull(response.body()).source());
sink.close();