Examples
Screenshot of a particular element 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 of the element and save to file
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");
page.locator("#example-card").screenshot(
new Locator.ScreenshotOptions().setPath(Paths.get("element.png")));
}
}
} Find out how to take a full-page screenshot with Java and Playwright.
That was 14 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.