How to Take a Screenshot in Selenium (Java and Python)

screenshot in selenium

Screenshots make test failures easier to debug. Here's how to take one in Selenium with Java and Python, plus a no-code option in Katalon Studio.

Key takeaways
The 30-second version of this article
Screenshots show exactly what the browser displayed when a test failed, so you can debug without rerunning the test by hand.
In Java, cast the driver to TakesScreenshot and call getScreenshotAs(). In Python, call driver.save_screenshot().
Since Selenium 4.6, Selenium Manager downloads browser drivers automatically, so you no longer need System.setProperty or webdriver-manager.
In Katalon Studio, add the Take Screenshot keyword to a recorded test, with no code needed.

Screenshots make test failures much easier to debug. Selenium WebDriver can capture the browser at any point during a test run.

This guide covers 3 ways to take a screenshot:

  • Guide 1: Selenium with Java
  • Guide 2: Selenium with Python
  • Guide 3: Katalon Studio

You'll need the Selenium library installed first. If you don't have it yet, follow the Selenium installation guide.

Why take a screenshot in Selenium?

When a test fails, you need to know why, without rerunning it by hand. A screenshot shows exactly what the browser displayed at the moment of failure. You can spot a broken component, an error message, or the wrong page at a glance.

Screenshots also prove that each step of a flow looked right, which speeds up reviews and debugging.

Take a screenshot when:

  • The application behaves unexpectedly
  • An assertion fails
  • Selenium can't find an element
  • A wait times out before an element loads
  • You need a visual record of the UI

What you need before you start

Before you start, you need:

  • Selenium WebDriver: drives the browser. You need a running WebDriver instance to take a screenshot.
  • A browser driver: such as ChromeDriver for Chrome or GeckoDriver for Firefox. Since Selenium 4.6, Selenium Manager downloads the right driver automatically, so you don't need to set a driver path or install a separate driver manager.
  • The screenshot method for your language: getScreenshotAs() in Java, or save_screenshot() in Python.
  • A save location: a folder your script can write to.

How does Selenium take a screenshot?

  1. Your script asks WebDriver for a screenshot.
  2. The browser driver captures the visible page as a PNG.
  3. Selenium returns the image, and your script saves it or attaches it to a report.

The screenshot shows exactly what's on screen at that moment, including pop-ups, error messages, overlays, or a page that hasn't finished loading.

Guide 1. Take a screenshot in Selenium using Java

In Java, use Selenium's TakesScreenshot interface:

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

Here's a complete example:

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScreenshotExample {
    public static void main(String[] args) {
        // Selenium Manager (Selenium 4.6+) finds and downloads ChromeDriver for you
        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 the code does:

  1. Starts Chrome. Selenium Manager handles the driver.
  2. Opens the page with driver.get.
  3. Casts the driver to TakesScreenshot and captures the page as a file.
  4. Sets the output path. Replace /path/to/screenshot.png with your own location.
  5. Saves the file with FileUtils.copyFile (from Apache Commons IO).
  6. Catches any IOException while saving.
  7. Closes the browser in the finally block.

Guide 2. Take a screenshot in Selenium using Python

In Python, use the save_screenshot() method:

driver.save_screenshot("screenshot.png")

Here's a complete example:

from selenium import webdriver

# Selenium Manager (Selenium 4.6+) finds and downloads ChromeDriver for you
driver = webdriver.Chrome()

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 the code does:

  1. Starts Chrome. Selenium Manager handles the driver.
  2. Opens the page with driver.get.
  3. Saves the screenshot with driver.save_screenshot(), using the path in screenshot_path.
  4. Closes the browser in the finally block, even if something fails.

Guide 3. Take a screenshot with Katalon Studio

The free version of Katalon Studio includes Record-and-Playback. You perform the steps by hand, and Katalon turns them into an automated test. You can then add keywords from Katalon's library or switch to Script mode for full control.

Download Katalon Studio for free

Here's how to build a test that takes a screenshot of a YouTube search. After you install Katalon Studio, go to File > New > Test Case.

Create a new test case in Katalon Studio

Give your test case a name:

Name a test case in Katalon Studio

Next, open Record-and-Playback. Enter the URL to record, choose a browser, and click Start.

Record and playback feature in Katalon Studio

Now perform your steps. Katalon records each one and turns it into an editable test. In this example, we recorded:

  1. Go to YouTube
  2. Type "Ted Talk"
  3. Click Search
Automate screenshot with Katalon Studio

Here's the recorded test in Katalon Studio. To add a step, click Add and fill in the Input column. Here we added the Take Screenshot keyword with the file path for the screenshot. If screenshots don't appear in your reports, see how to fix screenshots in Katalon reports.

Use Katalon Studio to take screenshot in Selenium

Prefer code? Switch to Script mode with the tab at the bottom. Here we added a TakeScreenshot command with the file path, followed by CloseBrowser.

Scripting mode in Katalon Studio

Every object you record is saved in the Object Repository, so you can reuse it across test cases. Test suites, data files, and screenshots are organized for you:

Object Repository in Katalon Studio

Screenshots help you:

  • Capture the page when a test fails
  • Check UI elements visually
  • Document test progress and results
  • Provide evidence for audits and compliance

Try it: capture screenshots of a real login flow in Katalon Studio

The fastest way to see the value is to test a flow your team already cares about, like logging in. In about 10 minutes, you can:

  1. Record the flow: open Record-and-Playback, sign in to your app, and stop the recording.
  2. Add checkpoints: insert a Take Screenshot step after the login page loads and another after the dashboard appears.
  3. Capture failures automatically: in Project Settings > Execution, turn on screenshots for failed steps, so every failure comes with a picture.
  4. Run it in more than one browser: run the same test in Chrome and Firefox, then compare the screenshots in the test report.

You get a reusable login test, plus visual proof of what each browser showed at every step. No Selenium setup or driver management needed.

Start capturing screenshots with Katalon Studio

Frequently asked questions

Huyen Nguyen
Huyen NguyenTechnical Writer, Katalon

Huyen Nguyen is an experienced technical writer in the software testing industry. With strong technical expertise and a deep understanding of Katalon products, she creates clear, practical guides that support testers at every skill level.

Connect on LinkedIn →
Get started

Test everything. Ship with confidence.

From generating test cases to spotting failures, Katalon's AI works across your entire pipeline, automatically.

Live webinar

The Next Evolution of Manual Testing: Introducing Katalon AI Test Runner

Learn how to grow your QA team's capacity in the age of AI, then watch Katalon AI Test Runner turn manual test cases into automated runs, live.

2:00 to 3:00 PM IST
Almas AhmedSenior Account Manager
Sumit WankhedeSolutions Engineer Manager
Register now