How to take website screenshots with Java

Updated on Dmytro Krasun 3 min read
Today, there are many options to make screenshots of any URL with Java. Let's examine them all and choose which suits you best.

Today, there are many options to make screenshots of any URL with Java:

  1. Selenium.
  2. Playwright library.
  3. Or Screenshot API as a service.

They might overlap, but there is no best solution. Each depends on your use case and requirements.

Selenium

Selenium is a well-known kid in the QA automation area, so it is easy to start taking screenshots if you plan to write automation tests or already do it.

For Maven, add the selenium-java dependency in your project pom.xml file:

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>

For Gradle, add the selenium-java dependency in your project build.gradle file:

dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0'
}

And then:

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;
import java.io.File;
public class App {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("http://www.example.com");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("./example.png"));
driver.quit();
}
}

If you just want to take one or two screenshots locally, the Selenium WebDriver client is not the best fit. As I wrote earlier, it better serves you already write automation tests with Selenium or plan to write them.

Playwright

Playwright is the best Java library to automate Chromium, Firefox, and WebKit with a unified API. It is built to enable cross-browser web automation.

If you want to take screenshots in different browsers, Playwright is the best choice.

Install the library:

<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.17.1</version>
</dependency>
</dependencies>

And then take a screenshot:

import com.microsoft.playwright.*;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) throws Exception {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.webkit().launch();
Page page = browser.newPage();
page.navigate("https://example.com/");
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("example.png")));
}
}
}

Playwright is the best choice. It is as powerful and allows you to run different browser instances if needed.

If you plan to take millions of screenshots and manage browser instances, you can do it yourself, but it is better to outsource to well-established services.

Screenshot API as a service

We specialize in taking screenshots and managing browser instances at scale. We provide a high-quality Java client to take screenshots and cover a variety of use cases.

Easy to install:

<dependencies>
<dependency>
<groupId>com.screenshotone.jsdk</groupId>
<artifactId>screenshotone-api-jsdk</artifactId>
<version>[1.0.0,2.0.0)</version>
</dependency>
</dependencies>

And easy to use:

import com.screenshotone.jsdk.Client;
import com.screenshotone.jsdk.TakeOptions;
import java.io.File;
import java.nio.file.Files;
public class App {
public static void main(String[] args) throws Exception {
final Client client = Client.withKeys("IVmt2ghj9TG_jQ", "Sxt94yAj9aQSgg");
TakeOptions takeOptions = TakeOptions.url("https://scalabledeveloper.com")
.fullPage(true)
.deviceScaleFactor(1)
.viewportHeight(1200)
.viewportWidth(1200)
.format("png")
.omitBackground(true);
final byte[] image = client.take(takeOptions);
Files.write(new File("./example.png").toPath(), image);
}
}

Or if you need to place just an URL inside the image tag, you can generate only the URL of the screenshot:

import com.screenshotone.jsdk.Client;
import com.screenshotone.jsdk.TakeOptions;
public class App {
public static void main(String[] args) throws Exception {
final Client client = Client.withKeys("IVmt2ghj9TG_jQ", "Sxt94yAj9aQSgg");
TakeOptions takeOptions = TakeOptions.url("https://scalabledeveloper.com")
.fullPage(true)
.deviceScaleFactor(1)
.viewportHeight(1200)
.viewportWidth(1200)
.format("png")
.omitBackground(true);
final String url = client.generateTakeUrl(takeOptions);
System.out.println(url);
// Output: https://api.screenshotone.com/take?url=...
}
}

If you feel that it is the best fit for you, feel free to sign up for our screenshot API and get the access key.

Summary

Pick the solution which suits your needs best. If you decide to go with our API, please ask any questions and mail us at support@screenshotone.com. And have a nice day 👋

By the way, you might also find interesting how to: