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.
- Guide 1: Screenshot in Selenium using Java
- Guide 2: Screenshot in Selenium using Python
- Guide 3: Screenshot in Selenium using Katalon Studio
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:
- We use a try block to contain the main logic for all of the actions below
- Use driver.get to navigate to the website (which is https://katalon.com/katalon-studio )
- Take a screenshot using the TakeScreenshot interface
- 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).
- Use FileUtils to save the file
- At the end, we added a catch block to catch any IOException that occurs during the file operations.
- The finally block to close the browser
The final result should give you a screenshot of the Katalon Studio page like this:
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();
}
}
}
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
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.
Let’s give our test case a name:
Now enter the Record and Playback mode. Specify the URL you want to record, choose the browser, and start recording.
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:
- Go to YouTube
- Type in “Ted Talk”
- Click Search
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.
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.
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:
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.