Capturing web page screenshot using WebDriver API


You can use the below snippet to take screenshot and save it where you want. Commons IO libraries can be downloaded here: http://commons.apache.org/io/download_io.cgi


...
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//Needs Commons IO library
FileUtils.copyFile(scrFile, new File("c:\\screenshot\\googlesearch-webdriverapi.png"));
...

2 comments

  1. One question though. How does the “File” type know what type of image this is ? Is it a png, jpg, bmp, what ?

    1. That was just the extension I preferred in the code. I guess, there isn’t any default type. You can either save it with JPEG or PNG extension or with whatever extension that image has in tag source. Irrespective of it, most image viewers these days are capable of rendering it even if the extension is wrong. Your thoughts?

Leave a comment