Selenium is one of the most popular automation testing framework and has the capability to capture screenshot of a web page. It runs on all major browsers. So, in this particular example, we are going to use Google Chrome. As it can be used in various programming languages like Ruby, Python, Java etc. Hence, In this post, we are going to use it to learn “how to take a web page screenshot in JAVA?”.

Take a Web page screenshot in Java using Selenium Webdriver...!!! Share on X
In this tutorial, we are going to cover below topics:
-
Configure Selenium Webdriver and other required dependencies in the project
-
How to take a screenshot of a web page in Java?
Let’s begin:
1. Add required dependencies into the Project
As we are using Selenium Webdriver, there we first need to configure it into our project either by downloading the Selenium Java Jar file and add it into the classpath or specify the below maven dependency into your maven project.
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.0.0-alpha-2</version> </dependency>
Also, add below supported dependencies
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>3.6.2</version> </dependency>
2. Capture Web Page Screenshot in Java
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
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;
import io.github.bonigarcia.wdm.WebDriverManager;
public class WebPageScreenshotInJavaUsingSeleniumWebdriver {
static WebDriver driver;
public static void main(String[] args) throws IOException {
//Set ChomeDriver as we are using Chrome Browser in our Example.
//Either set the ChromeDriver using traditional way by setting it using system property with hardcoded location like below
//System.setProperty("webdriver.chrome.driver","C://driver/chromedriver.exe");
//Or Use WebDriverManager to setup the chromedriver like below
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
//Specify the targetted URL
String testURL = "https://www.google.com";
//Access the targetted URL
driver.get(testURL);
//Capture screenshot of the web page using TakesScreenshot method
final File screenShotOutputFile = new File("screenshot_" + generatetimeStampBasedRandomNumber() + ".png").getAbsoluteFile();
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, screenShotOutputFile);
System.out.println("Took Screenshot for " + testURL + " saved at " + screenShotOutputFile);
//Quit the driver
driver.quit();
}
//Method to generate Random number based on DateTimeStamp
private static String generatetimeStampBasedRandomNumber() {
Date date = new Date();
long time = date.getTime();
Timestamp ts = new Timestamp(time);
String tst = ts.toString();
try {
tst = tst.substring(0, tst.length() - 4);
tst = tst.replace("-", "");
tst = tst.replace(" ", "");
tst = tst.replace(":", "");
} catch (Exception e) {
e.printStackTrace();
}
return tst;
}
}
Let’s try to understand the code:
1. Set up the Chrome Driver
WebDriverManager.chromedriver().setup(); driver = new ChromeDriver();
2. Specify the targeted URL and access it through the driver object
String testURL = "https://www.google.com"; driver.get(testURL);
3. Capture screenshot with the help of TakesScreenshot Webdriver Interface and save it
final File screenShotOutputFile = new File("screenshot_" + generatetimeStampBasedRandomNumber() + ".png").getAbsoluteFile();
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, screenShotOutputFile);
Take a Web page screenshot in Java using Selenium Webdriver...!!! Share on X
Do you like this Post? – then check my other helpful posts:
Other Useful References:
driver = new ChromeDriver();
Getting an exception when executing this line, how to resolve that. Please help me.
It will difficult to help you out unless I know the whole exception.
Please share the exception log that you are getting so that I could try to help you.
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information.
Thank you.