Take a Screenshot of Web Page

3 years ago by in How To, Miscellaneous Tagged:

Selenium () 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 (), 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.

Albert Attard

Albert Attard is a Java passionate and technical lead at a research group. You can find him on . Over the past years Albert worked on various Java projects including traditional server/client applications, modular applications, large data handling applications and concurrent data manipulation applications to name a few. He has a BSc degree from the University of London (Homepage) and an MSc Information Security with the same university. His MSc thesis (Book) received the 2012 SearchSecurity.co.UK award (Website).

Leave a Comment

Time limit is exhausted. Please reload the CAPTCHA.