Take a Screenshot of Web Page

Selenium (Homepage) is a comprehensive testing framework for web applications which allows us to take screenshots of web pages programmatically amongst many other things. It makes use of web browsers, such as FireFox (Homepage), to open the web page and then takes the screenshot. This guarantees a genuine representation of the web page.

The following example illustrates how to do this.

package com.javacreed.examples.misc;

import java.io.File;
import java.util.concurrent.TimeUnit;

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.firefox.FirefoxDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Example {

  public static final Logger LOGGER = LoggerFactory.getLogger(Example.class);

  public static void main(final String[] args) throws Exception {
    final String link = "http://www.javacreed.com/";
    final File screenShot = new File("screenshot.png").getAbsoluteFile();

    Example.LOGGER.debug("Creating Firefox Driver");
    final WebDriver driver = new FirefoxDriver();
    try {
      Example.LOGGER.debug("Opening page: {}", link);

      Example.LOGGER.debug("Wait a bit for the page to render");

      Example.LOGGER.debug("Taking Screenshot");
      final File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(outputFile, screenShot);
      Example.LOGGER.debug("Screenshot saved: {}", screenShot);
    } finally {


The delay of five seconds was only required as the page renders the latest posts using JavaScript after the page is loaded. Thus without this delay we will not see the final result.

The code show above is available at: https://github.com/javacreed/take-a-screenshot-of-webpage. The readers can download or view all code from the above link.

The coder illustrated above makes use of the following Maven dependencies.


Kindly note that the versions used here may not be the latest ones.