Today, there are many options to make screenshots of any URL with Java. Let's examine them all and choose which suits you best.
How to take website screenshots with Java
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:
They might overlap, but there is no best solution. Each depends on your use case and requirements.
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 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.
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.
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:
Interviews, tips, guides, industry best practices, and news.
Let's talk about the optimizeForSpeed parameter introduced in Chrome DevTools Protocol which will soon be supported by Puppeteer or even supported now at the time when you are reading the post.
Check out how ScreenshotOne relies on Cloudflare Workers as an API gateway to enhance performance, reliability, and cost-efficiency.
From today, you can render screenshots of any website or even raw HTML in ChatGPT—ScreenshotOne is available as a plugin. But at the moment of writing not available yet, in the official store, but it can be available when you read it.
Exhaustive documentation, ready SDKs, no-code tools, and other automation to help you render website screenshots and outsource all the boring work related to that to us.