Automation, Automation Framework, Selenium, System Testing, Testing, Web Automation, Web Testing

How to take screenshots via Selenium.

If we have to prove something to either a developer or a Product owner then we generally we have several ways via which we can actually show the idea to them, but what if whatever you want to show stop working when you actually want to demo it? This is silly, right? But it happens, You have to believe me!!

So for such circumstances we search for back up which will show what exactly we want to show. And that’s where we require screen shot, there could be other reasons as well, I am Sure there are.. but the major is to give other person a clear picture of what happened and how? Selenium also provides certain features via which we can take screen shots of the tests here is the small code to do it.

public static void main(String a[]) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
try {
WebElement searchBox = driver.findElement("q"));
searchBox.sendKeys("WebDriver API");
WebElement searchButton = driver.findElement("btnG"));;
System.out.println("What's the current Url: " + driver.getCurrentUrl());
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\googlesearch-webdriverapi.png"));
} catch (Exception e) {
e.printStackTrace(); // For debugging purposes

Lets analyze the code more:-
Go to Google Home Page, or open an  URL.


Look for search textbox and enter search term there

WebElement searchBox = driver.findElement(“q”));

This is not required or recommended any where, but just wait for the last click() operation to get completed fine.


If you wish to take screen shot of this page, you can!

File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(“c:\\googlesearch-webdriverapi.png”));

Close the driver, once you’re done.



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