Implementation:Puppeteer Puppeteer Page Screenshot
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Visual_Testing |
| Last Updated | 2026-02-11 23:00 GMT |
Overview
Concrete tool for capturing page screenshots as image files, provided by the puppeteer-core library.
Description
The Page.screenshot() method captures a screenshot of the current page state. It supports multiple output formats, full-page capture, region clipping, and both binary and base64 output encodings. The implementation handles viewport resizing for full-page captures and delegates to the browser's built-in screenshot API (CDP's Page.captureScreenshot or BiDi's browsingContext.captureScreenshot).
Usage
Call this method after the page has finished loading and any dynamic content has rendered. Pass a path option to save directly to a file, or capture the return value for in-memory processing.
Code Reference
Source Location
- Repository: puppeteer
- File: packages/puppeteer-core/src/api/Page.ts
- Lines: 2625-2746
Signature
class Page {
screenshot(
options: Readonly<ScreenshotOptions> & {encoding: 'base64'}
): Promise<string>;
screenshot(options?: Readonly<ScreenshotOptions>): Promise<Uint8Array>;
}
Import
// Accessed through a Page instance
const page = await browser.newPage();
await page.screenshot(options);
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| options | ScreenshotOptions | No | Screenshot configuration |
| options.path | string | No | File path to save the screenshot |
| options.type | 'png' or 'jpeg' or 'webp' | No | Image format (default: 'png') |
| options.fullPage | boolean | No | Capture the full scrollable page (default: false) |
| options.clip | ScreenshotClip | No | Region to capture {x, y, width, height} |
| options.quality | number | No | JPEG/WebP quality 0-100 (not applicable to PNG) |
| options.encoding | 'binary' or 'base64' | No | Output encoding (default: 'binary') |
| options.omitBackground | boolean | No | Make default background transparent (default: false) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Promise<Uint8Array> or Promise<string> | Screenshot as binary data or base64 string depending on encoding option |
Usage Examples
Basic Screenshot
const puppeteer = require('puppeteer');
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
Full Page Screenshot With Device Emulation
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulate(puppeteer.devices['iPhone 6']);
await page.goto('https://www.nytimes.com/');
await page.screenshot({path: 'full.png', fullPage: true});
await browser.close();
Screenshot As Base64
const base64 = await page.screenshot({encoding: 'base64'});
console.log(`data:image/png;base64,${base64}`);
Clip A Specific Region
await page.screenshot({
path: 'clipped.png',
clip: {x: 0, y: 0, width: 800, height: 600},
});