How to take a screenshot with Selenium WebDriver

By FoxLearn 1/10/2025 8:42:50 AM   34
Taking screenshots in Selenium WebDriver is a key feature for automated testing, allowing testers to capture visual evidence of issues, bugs, or test failures.

It helps in debugging, validating the flow of an application, and ensuring the accuracy of tests without needing to re-run the entire test each time.

Why Take Screenshots in Selenium?

Screenshots are essential in various testing scenarios, including:

  • When application issues or assertion failures occur.
  • When difficulties arise in finding web elements or encountering timeouts.
  • For verifying the expected behavior of the application.

With the WebDriver architecture, Selenium allows you to capture screenshots of the entire page, visible segments, or even specific HTML elements.

How to Take a Screenshot in Selenium WebDriver

Before diving into the code, make sure you have the following prerequisites set up:

  1. Selenium WebDriver: Ensure that you have Selenium WebDriver installed in your project.
  2. Visual Studio: You can use Visual Studio or any other C# IDE to write your scripts.
  3. WebDriver Browser Driver: Ensure that you have the necessary WebDriver (e.g., ChromeDriver, FirefoxDriver) installed.

You can install Selenium WebDriver in your C# project by using NuGet Package Manager:

Install-Package Selenium.WebDriver

Also, if you're using a browser like Chrome, you may need the ChromeDriver. This can be installed similarly:

Install-Package Selenium.WebDriver.ChromeDriver

To capture a screenshot, Selenium uses the TakesScreenshot interface.

public static void CaptureScreenshot(IWebDriver driver)
{
    // Cast WebDriver to ITakesScreenshot
    ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;

    // Take a screenshot
    Screenshot screenshot = screenshotDriver.GetScreenshot();

    // Define the file path where the screenshot will be saved
    string filePath = @"C:\screenshot.png";

    // Save the screenshot
    screenshot.SaveAsFile(filePath, ScreenshotImageFormat.Png);
}

Creating a simple WebDriver instance.

For this example, we’ll use ChromeDriver, but you can easily replace it with FirefoxDriver or EdgeDriver.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;

class Program
{
    static void Main()
    {
        // Initialize ChromeDriver
        IWebDriver driver = new ChromeDriver();

        // Navigate to a website
        driver.Navigate().GoToUrl("https://www.example.com");

        // Call the method to capture a screenshot
        CaptureScreenshot(driver);

        // Close the driver
        driver.Quit();
    }
}

The CaptureScreenshot method uses ITakesScreenshot.GetScreenshot() to capture the screenshot and then saves it in the specified location. The screenshot is saved in PNG format by default, but you can choose other formats like JPG if needed.

Taking screenshots using Selenium WebDriver in C# is a simple and powerful way to aid in web testing, debugging, and visual verification.