在 java 中使用 Selenium 截取包括任务栏的屏幕截图

Take screenshot including Task bar using Selenium in java

我需要使用 java 截取页面(Web 应用程序)的屏幕截图,包括 Windows Selenium 中的任务栏。谁能告诉我该怎么做。

我正在使用下面的代码截取屏幕截图,但是我也需要截取任务栏的屏幕截图。基本上我想使用 Selenium 重新创建 "Print Screen (PrntSc)" 功能。

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:\screenshot.png"));

试试下面的代码:

public static void captureScreen() throws AWTException, UnsupportedFlavorException, IOException{

        Robot robot = new Robot();

        robot.keyPress(KeyEvent.VK_PRINTSCREEN);
        robot.keyRelease(KeyEvent.VK_PRINTSCREEN);

        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();


        BufferedImage img = (BufferedImage) cb.getData(DataFlavor.imageFlavor);
        File file = new File("C:/newimage.png");
        ImageIO.write(img, "png", file);

    }

如前所述,这个问题已经被问过了。 但是,为了方便起见,这里是解决方案。

只需使用 Selenium,您就可以只截取浏览器的屏幕截图 DOM window。您将需要机器人 API 来满足 Java 的要求,不需要第三方 API。

代码如下:

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = new Robot().createScreenCapture(screenRect);
ImageIO.write(screenFullImage, "png", new File("./Screenshots/"+ FILENAME));

Selenium 仅适用于浏览器 DOM window,它无法在 DOM window.

之外的任何内容上运行

有关这方面的更多信息,请参阅此线程:Is there a way to take a screenshot using Java and save it to some sort of image?