在java中使用Selenium拍摄包含任务栏的屏幕截图
我需要使用java在Selenium中拍摄页面(web应用程序)的屏幕截图,包括Windows任务栏。谁能告诉我怎么做吗 我用下面的代码来截图,但我也需要截图的任务栏。基本上,我想使用Selenium重新创建“打印屏幕(PrntSc)”功能在java中使用Selenium拍摄包含任务栏的屏幕截图,java,selenium,selenium-webdriver,ui-automation,Java,Selenium,Selenium Webdriver,Ui Automation,我需要使用java在Selenium中拍摄页面(web应用程序)的屏幕截图,包括Windows任务栏。谁能告诉我怎么做吗 我用下面的代码来截图,但我也需要截图的任务栏。基本上,我想使用Selenium重新创建“打印屏幕(PrntSc)”功能 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File("C:\\screenshot.p
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窗口的屏幕。您将需要RobotAPI来满足需求,它是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窗口上工作,无法在DOM窗口之外的任何对象上运行
有关这方面的更多信息,请参阅此线程:可能重复的