Dev Malhotra
Dev Malhotra

Reputation: 31

Take screenshot including Task bar using Selenium in java

I need to take the screenshot of the page(web application) including Windows Taskbar in Selenium using java. Can anyone please tell me how to do it.

I am using below code to take the screenshot however i need to take a screenshot of Taskbar too. Basically i want to recreate "Print Screen (PrntSc)" functionality using Selenium.

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

Upvotes: 2

Views: 2259

Answers (2)

Apurv Chatterjee
Apurv Chatterjee

Reputation: 175

As already mentioned this question has already been asked. However, just for the convenience, here is the solution.

Simply using Selenium, will let you take screenshot only of the browser DOM window. You'll be needing Robot API for the requirement, which is native to Java, no third-party API required.

The code is as below:

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

Selenium just works on the browser DOM window, which fails to operate on anything outside the DOM window.

For more information on this, please refer this thread: Is there a way to take a screenshot using Java and save it to some sort of image?

Upvotes: 1

Kushal Bhalaik
Kushal Bhalaik

Reputation: 3384

Try following code:

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);

    }

Upvotes: 0

Related Questions