2

我正在尝试使用 selenium 将 .html 页面保存到我的桌面中。

有人可以帮忙吗?

4

2 回答 2

5

您可以使用高级用户交互 API

new Actions(driver)
    .sendKeys(Keys.chord(Keys.CONTROL, "s"))
    .perform();

这将打开另存为对话框,然后您需要在其中手动导航/使用 Java 的Robot类:

Robot robot = new Robot();
// press Ctrl+S the Robot's way
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_S);
// press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Ctrl+ S, Enter。请注意,这不适用于每个系统和/或浏览器。

于 2013-07-19T08:13:11.947 回答
-1
JavascriptExecutor js = (JavascriptExecutor) driver;                              
String base64string = (String) js.executeScript("var c = document.createElement('canvas');"
    + " var ctx = c.getContext('2d');"
    + "var img = document.getElementsByTagName('img')[0];"
    + "c.height=img.naturalHeight;"
    + "c.width=img.naturalWidth;"
    + "ctx.drawImage(img, 0, 0,img.naturalWidth, img.naturalHeight);"
    + "var base64String = c.toDataURL();"
    + "return base64String;");
String[] base64Array = base64string.split(",");

String base64 = base64Array[base64Array.length - 1];

byte[] data = Base64.decode(base64);

ByteArrayInputStream memstream = new ByteArrayInputStream(data);

BufferedImage saveImage = ImageIO.read(memstream);

ImageIO.write(saveImage, "png", new File("path"));
于 2020-04-06T13:59:28.437 回答