How To Take Screenshot in Selenium?

April 23, 2026 · 8 min read · Tool Comparison

Blog / Insights /
How To Take Screenshot in Selenium?

How To Take Screenshot in Selenium?

Contributors Updated on

Learn with AI

Linkedin

Facebook

X (Twitter)

Mail

Learn with AI

Taking screenshots during automated testing is an significant & nbsp; practice for debugging and coverage purposes. Selenium WebDriver provides built-in functionality to capture screenshots of web pages at any point in the exam execution.

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

  • Guide 1:
  • Guide 2:
  • Guide 3:

Make sure you already have the Selenium library instal. If not, refer to theSelenium documentationto set it up.

All good? Let 's get proceed!

Why take screenshot in Selenium?

Machine-driven testing loses much of its value when failures require repeat manual reruns to diagnose what went wrong. A test may fail for many understanding, and without a visual reference, pinpointing the underlying cause become unnecessarily time-consuming. A screenshot cater an immediate, reliable snapshot of the browser at the exact moment the issue occurred, helping examiner quickly spot broken UI component, unexpected behaviors, or wrong page states. Screenshots also serve as visual proof that each portion of the application acquit harmonise to the wait workflow, making them a key plus for smoother establishment and faster debugging.

This is precisely why enchant screenshots in Selenium is so important.

Here are some mutual situations where taking a screenshot in Selenium becomes essential:

  • When the application behaves unexpectedly
  • When an asseveration fails during a test
  • When Selenium scramble to locate web elements
  • When a timeout pass while waiting for elements to load
  • When you need a clear optical reference to substantiate UI behavior

What do you need before taking & nbsp; screenshot in Selenium?

Taking screenshots in Selenium with Python requires a few indispensable component to be set up beforehand. Here ’ s what you ask before you can depart capturing images successfully:

  • Selenium WebDriver:This is the foundation of all browser mechanisation in Python. WebDriver allow you to open page, interact with ingredient, perform actions, and ultimately take screenshots. Without format a WebDriver instance, no screenshot functionality can be used.
  • save_screenshot () Method:In Python, screenshots are captured habituate the built-in save_screenshot () method. This method lets you store the current state of the browser window as an persona file. It ’ s simple, authentic, and the standard way to take screenshots using Selenium in Python.
  • webdriver-manager (optional but recommended):Managing browser drivers manually can be tedious. The webdriver-manager library automatically downloads and configure the correct ChromeDriver or GeckoDriver version for you. This ensures your WebDriver setup works smoothly without version mismatches.
  • File Path for Saving the Screenshot:Before taking a screenshot, you need a valid destination path where the image will be store. Make certain the folder exists and your Python script has license to write to that directory.
  • Appropriate Browser Driver:Whether you ’ re using Chrome, Firefox, Edge, or another browser, you must have the like driver installed or deal (e.g., ChromeDriver for Chrome). The driver is what allows Selenium to communicate with the browser.

Once these requirement are in place, take a screenshot in Selenium with Python get a straightforward and reliable operation.

How make Selenium TakeScreenshot work?

  • Selenium becharm the browser ’ s current province– When a screenshot is triggered, Selenium communicate now with the browser through the WebDriver API to request an ikon of whatever is currently rendered on the screen.
  • WebDriver asks the browser driver to direct the screenshot– The browser driver (such as ChromeDriver or GeckoDriver) receives the command and deal the low-level work of becharm the visible area of the browser window.
  • The browser driver generates a raw image– The driver collects the pixel information rendered by the browser at that moment and packages it into a standard image format (usually PNG).
  • The icon is returned to Selenium– Once captured, the pixel data is legislate back through the WebDriver API as an image yield, which Selenium so exposes to your playscript.
  • The script saves the screenshot– Your code decides what to do with the captured image: relieve it to a file, convert it to base64, log it, or attach it to a report.
  • Screenshot termination depends on browser state– Whatever is presently visible on the screen (include pop-ups, errors, overlays, or incomplete page loads) is incisively what Selenium charm.

Guide 1. Take a screenshot in Selenium using Java

To take a screenshot in Java, use theTakesScreenshotinterface render by Selenium.

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

Import the necessary library:

Java
import org.openqa.selenium.WebDriver; significance 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 file.

Define the category and main method:

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

Set the ChromeDriver path (replace with your existent location):

Java
System.setProperty (`` webdriver.chrome.driver '', `` /path/to/chromedriver '');

Initialize the WebDriver:

Java
WebDriver driver = new ChromeDriver ();

Here ’ s the main codification that captures a screenshot:

Java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; signification org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.OutputType; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public stratum 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 relieve successfully! ``);} catch (IOException e) {System.out.println (`` An error occurred while saving the screenshot. ``); e.printStackTrace ();} finally {driver.quit ();}}}

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

What ’ s happening here is:

  1. We use a tryblock to contain the main logic.
  2. Use driver.getto navigate to the website ()
  3. Take a screenshot using theTakesScreenshotinterface.
  4. Specify the output file way: File destFile = new File (`` /path/to/screenshot.png ''); make sure to supercede path/to/screenshot with your actual terminus.
  5. Use FileUtilsto save the captured screenshot.
  6. Add a catch block to handle any IOException that may occur during file operations.
  7. The finallyblock shut the browser.

Guide 2. Take a screenshot in Selenium using Python

To occupy a screenshot in Python, use thesave_screenshot ()method ply by Selenium ’ s WebDriver API.

Python
driver.save_screenshot (`` screenshot.png '')

Import the necessary libraries:

Python
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options signification Options from webdriver_manager.chrome import ChromeDriverManager

Define the setup and initialise the WebDriver:

Python
service = Service (ChromeDriverManager () .install ()) driver = webdriver.Chrome (service=service, options=Options ())

Here ’ s the principal code that captures a screenshot:

Python
from selenium importation webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome meaning 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 salvage successfully! '') last: driver.quit ()

What ’ s pass here is:

  1. We configure the WebDriver service and initialize Chrome.
  2. Use driver.getto voyage to the prey webpage ().
  3. Call driver.save_screenshot ()to seizure and save the screenshot.
  4. Specify your preferred file path using thescreenshot_path variable.
  5. The tryblock ensures the page loads and screenshot is relieve.
  6. The finallyblock closes the browser after execution.

Guide 3. Take Screenshot with Katalon Studio

In the, you receive contiguous access to a Record-and-Playback feature. Simply perform your manual actions, and Katalon will mechanically convert them into an automation handwriting. From there, you can refine the script using Katalon ’ s extensive keyword library or switch to Scripting mode for maximum flexibility.

Let ’ s pass through how to create an automated script that takes a screenshot of a YouTube channel. After installing Katalon Studio, begin by navigating toFile & gt; New & gt; Test Caseto create a new one.

Give your test case a name:

Adjacent, 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 select a result. Katalon enamour each step and converts them into an editable hand. For example, the following actions were enter:

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

Here ’ s the script inside Katalon Studio. You can insert extra keywords by clicking the “ Add ” button and filling in the Input column. In this example, we added theTake Screenshotkeyword and provide the file path where the screenshot will be saved.

Prefer to cypher for more control? Switch to the Script mode using the tab at the hindquarters. There, enter the file route where you ’ d like your screenshot saved. In scripting mode, we added aTakeScreenshotcommand along with aCloseBrowser command.

The better part? Every object captured during transcription is store inside the Object Repository, which you can reuse across multiple test event and environments. Your test suites, information files, custom keywords, and yet screenshots are neatly organized following the Page Object Model. Here ’ s what it looks like:

Taking a screenshot is helpful when you desire to:

  • Capture the webpage state during test failures
  • Verify UI elements visually
  • Document test progress and results
  • Provide grounds for audits or deference
Explain

|

FAQs

Why should I lead screenshots during Selenium tests?

+

Screenshots aid youdebug failures, verify UI state, document test results, and provideevidence for audits/compliance—especially when a test betray intermittently or the UI is dynamic.

 

How do I lead a screenshot in Selenium using Java?

+

Use Selenium ’ sTakesScreenshotinterface:

  • Cast the driver toTakesScreenshot

  • Call getScreenshotAs (OutputType.FILE)

  • Copy the file to your target path (commonly withFileUtils.copyFile)
    This is typically wrapped intry/catch/finallyto handle IO erroneousness and assure the browser closes.

How do I take a screenshot in Selenium using Python?

+

Use the built-in method:

  • driver.save_screenshot (`` /your/path/screenshot.png '')
    Many squad pair this withwebdriver-managerso ChromeDriver versioning is handled automatically.

How can I capture screenshots utilize Katalon Studio (Selenium-based) without heavy steganography?

+

You can:

  • Record & amp; Playbackyour actions to generate steps automatically

  • Add the “ Take Screenshot ”keyword into the test case and ply a save path

  • Optionally trade toScripting modefor more control
    Captured objects are salvage to theObject Repositoryfor reuse across tests.

What are common better recitation for screenshot capture in UI automation?

+
  • Capture screenshotson failure(and optionally at key checkpoint).

  • Use unique filename(include test name + timestamp).

  • Save to a cognise artefact folderso CI/CD can archive it.

  • Pair screenshots withlogs(and video/HTML snapshot if available) for fast triage.

  • Avoid hardcoding OS-specific route when lam in CI (use env vars or relative route).

Contributors
The Katalon Team is composed of a diverse grouping of dedicated professional, including subject matter experts with deep domain noesis, receive technical author skilled, and QA specialiser who bring a practical, real-world perspective. Together, they contribute to the Katalon Blog, deliver high-quality, insightful articles that empower users to make the most of Katalon ’ s tools and stay update on the up-to-the-minute trend in test mechanization and software quality.

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free