Examples

Wait before taking screenshot with Java and Playwright

Add Playwright to your Maven project

<dependency>
  <groupId>com.microsoft.playwright</groupId>
  <artifactId>playwright</artifactId>
  <version>1.54.0</version>
</dependency>

Playwright downloads its browsers automatically on first run. Check Maven Central for the latest version.

Take a screenshot and save to file

Wait for the page to settle before taking the screenshot.

import com.microsoft.playwright.*;
import java.nio.file.Paths;

public class Screenshot {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch();
      Page page = browser.newPage();
      page.navigate("https://www.htmltoimage.com/demo");

      // Wait for late content — here an element that appears after 2 seconds.
      page.waitForSelector("#delayed-banner");

      page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("wait.png")));
    }
  }
}

Find out how to take a full-page screenshot with Java and Playwright.

That was 17 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.