15

How to simulate Print screen button using selenium web driver in Java

Regards, Vignesh

Guy
  • 46,488
  • 10
  • 44
  • 88
Vignesh
  • 521
  • 2
  • 6
  • 10
  • 1
    possible duplicate of [Take a screenshot using Selenium WebDriver with Java](http://stackoverflow.com/questions/3422262/take-a-screenshot-using-selenium-webdriver-with-java) – Seanny123 Sep 27 '13 at 10:40
  • Its actually a repeated question...he asked a clearer one earlier: http://stackoverflow.com/questions/19047675/how-to-take-screenshot-of-current-window-or-simulate-print-screen-using-selenium – Nathan Merrill Sep 27 '13 at 12:41
  • 1
    I tried the following code, WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com/");File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png")); But the problem with the code is it will take screenshot of the entire webpage , if say the webpage has 5 pages it will take screenshot of the all the 5 pages , but i want to take only the current page and i want to run this code in the backend hence i cant use Robot().createScreenCapture method as well – Vignesh Sep 28 '13 at 02:20
  • Maybe your question already has been answered [here](https://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver) – Abe Aug 23 '17 at 16:51

1 Answers1

4

selenium doesn't support it, only web page shots. However you can use Robot to do it

try {
    String format = "jpg";
    String fileName = printScreen." + format;

    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    Robot robot = new Robot();
    BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
    ImageIO.write(screenFullImage, format, new File(fileName));

} catch (AWTException | IOException ex) {
    System.err.println(ex);
}

And in C#

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(@"C:\ScreenShots\printScreen.jpg", ImageFormat.Jpeg);
Guy
  • 46,488
  • 10
  • 44
  • 88