Home > Selenium, Test Automation > Taking a Screenshot using Selenium WebDriver in C#

Taking a Screenshot using Selenium WebDriver in C#

It is always good to capture the screenshot of the test results if you are running the tests overnight. It is very easy and you may explicitly set the areas were you want to take the screenshot during the execution of automated tests and specify the save location.


  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NUnit as Unit Testing Tool
  • Selenium as a Test Automation Tool

Let me consider the test:

Given I am on the Google home page
When I search for text selenium
Then I should see the search results

I would be interested to take the snapshot in 2 locations:

  • When the system inputs the search text in the search field
  • Displaying of search results screen
  • Refer my previous blog on Set-up instructions.
  • Copy and paste the code snippet below into your Visual Studio 2010.
  • Run the code using NUnit.
  • Visit the image save location to view the screenshots (ex: C:\Users\anoops\Desktop\Screenshots in my case)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System.Drawing.Imaging;

namespace TestAutomation
 public class Driver
   IWebDriver driver;

   public void TakeScreenshot(IWebDriver driver, string saveLocation)
   ITakesScreenshot ssdriver = driver as ITakesScreenshot;
   Screenshot screenshot = ssdriver.GetScreenshot();
   screenshot.SaveAsFile(saveLocation, ImageFormat.Png);

   public void Setup()
   // Create a new instance of the Firefox driver
   driver = new FirefoxDriver();

   public void Teardown()

   public void GoogleSearch()
   //Navigate to the site
   // Find the text input element by its name
   IWebElement query = driver.FindElement(By.Name("q"));
   // Enter something to search for
   //Take screenshot during the input of search text
   TakeScreenshot(driver, @"C:\Users\anoops\Desktop\Screenshots\SearchInput.png");
   // Now submit the form
   // Google's search is rendered dynamically with JavaScript.
   // Wait for the page to load, timeout after 10 seconds
   WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
   wait.Until((d) => { return d.Title.StartsWith("selenium"); });
   //Check that the Title is what we are expecting
   Assert.AreEqual("selenium - Google Search", driver.Title);
   //Take screenshot after the dispay of search results
   TakeScreenshot(driver, @"C:\Users\anoops\Desktop\Screenshots\SearchResults.png");
  1. October 1, 2012 at 7:29 am

    Wow its great

  2. Ram
    October 17, 2012 at 7:29 pm

    Thank you Anoop, this is wonderful.
    There is selenium logging for viewing results in java.
    Is there any thing similar that can be used for logging results for c# Nunit.
    I am aware of XML – is there any thing that is more detailed than XML.

    • October 18, 2012 at 6:08 am

      I fear the is no Selenium logging utility in .net environment. I will keep you posted if I find anything in this space.

  3. Ramakrishna
    July 24, 2013 at 1:30 pm

    Thank you very much Anoop, it was really helpful to me to start.

    March 6, 2014 at 9:14 am

    Anoop i really impressed with your explanation.Good keep it up.

  1. No trackbacks yet.

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 )

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: