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!
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:
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();
}
}
}
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.
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:
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: