Drive QA's evolution—share your voice and win prizes up to $3,000
TAKE THE SURVEY
All All News Products Insights DevOps and CI/CD Community
Table of Contents

How To Take Screenshot in Selenium? A Comprehensive Guide

 

Here’s an easy-to-understand and straight-to-the-point guide to take a screenshot in Selenium.

Make sure that you have already installed a Selenium library. If you haven't, you can have a look at Selenium documentation to see how.

 

All good? Let's get going!

 

Guide 1. Take a screenshot in Selenium using Java

To take a screenshot in Java, you need to use the TakeScreenshot interface provided by Selenium. 
 

TakesScreenshot screenshot = (TakesScreenshot) driver; 
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);

 

Let’s assemble the script. First, we need to import the necessary libraries: 
 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils; // This utility class is used to save files.

Then we define public class ScreenshotExample and add the main method that will be executed when the program runs:

public class ScreenshotExample {
    public static void main(String[] args) {

Next we set up the ChromeDriver path. Make sure to replace the /path/to/chromedriver with your actual path to the chromedriver executable. For example, it can be “/Users/myname/Downloads/chromedriver_mac64/chromedriver”

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

Now we can initialize the the WebDriver:

WebDriver driver = new ChromeDriver();

Here’s the main piece of code to do take the screenshot:

        try {
            // Open a webpage
            driver.get("https://katalon.com/katalon-studio");
            
            // Take a screenshot and save it to a file
            TakesScreenshot screenshot = (TakesScreenshot) driver;
            File srcFile = screenshot.getScreenshotAs(OutputType.FILE); // Take screenshot
            
            // Specify the destination file path
            File destFile = new File("/path/to/screenshot.png");
            
            // Use FileUtils to save the file to the specified location
            FileUtils.copyFile(srcFile, destFile);
            
            System.out.println("Screenshot saved successfully!");
        } catch (IOException e) {
            System.out.println("An error occurred while saving the screenshot.");
            e.printStackTrace();
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

What’s happening here is:

  1. We use a try block to contain the main logic for all of the actions below
  2. Use driver.get to navigate to the website (which is https://katalon.com/katalon-studio )
  3. Take a screenshot using the TakeScreenshot interface
  4. Specify the file path with File destFile = new File("/path/to/screenshot.png"); (make sure to replace the path/to/screenshot with the desired path you want to store).
  5. Use FileUtils to save the file
  6. At the end, we added a catch block to catch any IOException that occurs during the file operations. 
  7. The finally block to close the browser

The final result should give you a screenshot of the Katalon Studio page like this:

screenshot in Selenium written with Java

Here's the full piece of code for you:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class ScreenshotExample {
    public static void main(String[] args) {
        // Set ChromeDriver path
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();
        try {
            // Open a webpage
            driver.get("https://katalon.com/katalon-studio");
            // Take a screenshot and save it to a file
            TakesScreenshot screenshot = (TakesScreenshot) driver;
            File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
            // Specify the destination file path
            File destFile = new File("/path/to/screenshot.png");
            // Use FileUtils to save the file to the specified location
            FileUtils.copyFile(srcFile, destFile);
            System.out.println("Screenshot saved successfully!");
        } catch (IOException e) {
            System.out.println("An error occurred while saving the screenshot.");
            e.printStackTrace();
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

Move to Katalon for the power of Selenium minus the complexities

Guide 2. Take a screenshot in Selenium using Python

Taking a screenshot in Selenium with Python is quite straightforward. Here’s the code for you:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
# Initialize the WebDriver with WebDriver Manager
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
try:
    driver.get('https://katalon.com/katalon-studio')
    screenshot_path = '/Users/hy.nguyen/Downloads/screenshot.png'
    driver.save_screenshot(screenshot_path)
    print(f'Screenshot saved at {screenshot_path}')
finally:
    driver.quit()

Here we use the webdriver-manager library to automatically download and install the correct version of ChromeDriver that matches the version of Google Chrome installed on your system. 
 

The Service(executable_path=ChromeDriverManager().install()) creates a Service object that manages the ChromeDriver’s life cycle, specifying where to find the ChromeDriver executable (executable_path). 
 

The webdriver.Chrome(service=service, options=chrome_options) initializes the Chrome WebDriver, which controls the Chrome browser. 
 

After that, with the try block, we use driver.get to open the https://katalon.com page, specify where we want to save the screenshot with screenshot_path variable, then use driver.save_screenshot function to simultaneously capture the screenshot and save it to the designated path. 
 

 

Guide 3. Take a screenshot in Selenium using Katalon Studio

Katalon logo

In the Katalon Studio free version, you immediately have access to a Record-and-Playback feature. Simply perform all of the manual actions and Katalon can turn all of that into an automation script. After that, you can edit it either using the rich keyword library that Katalon offers or enter Scripting mode for maximum flexibility. 
 

Download Katalon To Automate Test Steps Easily  
 

Let’s see how we can create an automated script to take the screenshot of Vsauce’s YouTube channel. After downloading the latest version of Katalon Studio, you can go to File > New > Test Case to create a new one. 
 

Create a new test case in Katalon Studio 
 

Let’s give our test case a name:

Name a test case in Katalon Studio for take a screenshot test case 
 

Now enter the Record and Playback mode. Specify the URL you want to record, choose the browser, and start recording.

Record and playback feature in Katalon Studio

 

Perform your actions, including typing in the name of the channel ad clicking Enter. Katalon registers your actions and turns into a fully editable script. For example, here we are performing the following actions:

  1. Go to YouTube
  2. Type in “Ted Talk”
  3. Click Search

automate screenshot with Katalon Studio

 

Here’s the result in Katalon Studio. You can add many more keywords as you wish simply by clicking the “Add” button and provide the data in the Input column. Here we added the Take Screenshot keyword and provide the path to which the screenshot is saved. 

Use Katalon Studio to take screenshot in Selenium

 

Want to code for maximum flexibility? Simply switch the mode by changing the tab at the bottom. Simply type in the file path you want your screenshot to be saved to. Now you have successfully built a script to automate screenshotting in Selenium with Katalon Studio. Here we added a TakeScreenshot command and a CloseBrowser command.

scripting mode in Katalon Studio 
 

The coolest thing about this? All of the test objects you captured during the process are stored in a single Object Repository that you can reused across test cases and test environments. Your test suites, data files, custom keywords you created, and even screenshots like this are also stored there in a tree view following the Page Object Model. Here's the result: 
 

Object Repository in Katalon Studio

Taking a screenshot is necessary for when you want to:

  • Capture webpage state during test failure.
  • Ensure UI elements appear correctly.
  • Document test progress and results.
  • Provide evidence for regulatory or audit purposes.

 

Try Screenshot Testing For Free  
 

Click