Taking screenshots during automated testing is an important practice for debugging and reporting purposes. Selenium WebDriver provides built-in functionality to capture screenshots of web pages at any point in the test execution.
Here’s an easy-to-understand and straight-to-the-point guide to taking a screenshot in Selenium.
Make sure you already have the Selenium library installed. If not, refer to the Selenium documentation to set it up.
All good? Let's get going!
Automated testing loses much of its value when failures require repeated manual reruns to diagnose what went wrong. A test may fail for many reasons, and without a visual reference, pinpointing the underlying cause becomes unnecessarily time-consuming. A screenshot provides an immediate, reliable snapshot of the browser at the exact moment the issue occurred, helping testers quickly spot broken UI components, unexpected behaviors, or incorrect page states. Screenshots also serve as visual proof that each part of the application behaves according to the expected workflow, making them a key asset for smoother validation and faster debugging.
This is exactly why capturing screenshots in Selenium is so important.
Here are some common situations where taking a screenshot in Selenium becomes essential:
Taking screenshots in Selenium with Python requires a few essential components to be set up beforehand. Here’s what you need before you can start capturing images successfully:
Once these prerequisites are in place, taking a screenshot in Selenium with Python becomes a straightforward and reliable process.
To take a screenshot in Java, use the TakesScreenshot interface provided by Selenium.
TakesScreenshot screenshot = (TakesScreenshot) driver;
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
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; // Used to save files.
Define the class and main method:
public class ScreenshotExample {
public static void main(String[] args) {
Set the ChromeDriver path (replace with your actual location):
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
Initialize the WebDriver:
WebDriver driver = new ChromeDriver();
Here’s the main code that captures a screenshot:
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) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://katalon.com/katalon-studio");
TakesScreenshot screenshot = (TakesScreenshot) driver;
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
File destFile = new File("/path/to/screenshot.png");
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 {
driver.quit();
}
}
}
What’s happening here is:
To take a screenshot in Python, use the save_screenshot() method provided by Selenium’s WebDriver API.
driver.save_screenshot("screenshot.png")
Import the necessary libraries:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
Define the setup and initialize the WebDriver:
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=Options())
Here’s the main code that captures a screenshot:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=Options())
try:
driver.get("https://katalon.com/katalon-studio")
screenshot_path = "screenshot.png"
driver.save_screenshot(screenshot_path)
print("Screenshot saved successfully!")
finally:
driver.quit()
What’s happening here is:
In the Katalon Studio free version, you have immediate access to a Record-and-Playback feature. Simply perform your manual actions, and Katalon will automatically convert them into an automation script. From there, you can refine the script using Katalon’s extensive keyword library or switch to Scripting mode for maximum flexibility.
Download Katalon To Automate Test Steps Easily
Let’s walk through how to create an automated script that takes a screenshot of a YouTube channel. After installing Katalon Studio, begin by navigating to File > New > Test Case to create a new one.
Give your test case a name:
Next, enter Record-and-Playback mode. Specify the URL you want to record, select a browser, and click “Start” to begin recording.
Now perform your actions—such as navigating, typing a keyword, and selecting a result. Katalon captures each step and converts them into an editable script. For example, the following actions were recorded:
Here’s the script inside Katalon Studio. You can insert additional keywords by clicking the “Add” button and filling in the Input column. In this example, we added the Take Screenshot keyword and provided the file path where the screenshot will be saved.
Prefer to code for more control? Switch to the Script mode using the tab at the bottom. There, enter the file path where you’d like your screenshot saved. In scripting mode, we added a TakeScreenshot command along with a CloseBrowser command.
The best part? Every object captured during recording is stored inside the Object Repository, which you can reuse across multiple test cases and environments. Your test suites, data files, custom keywords, and even screenshots are neatly organized following the Page Object Model. Here’s what it looks like:
Taking a screenshot is helpful when you want to: