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:

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



  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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: