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

About these ads

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 Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: