Playwright takes a screenshot with one call, page.screenshot(), and a dozen options decide what ends up in the image. This guide runs each option that matters on Playwright 1.64.1 and shows what it really produced: the size of the image, what changed in it, and the cases where the result was not what the option's name suggests.
The short version: use fullPage for the whole page, clip for a region, locator.screenshot() for one element, and animations: 'disabled' with mask when you need the same image twice. Three traps came up in our tests: a fixed header lands wherever the page was scrolled to, clip below the fold fails unless fullPage is on, and a mask on text that changes width still changes the image.
How we tested
We installed playwright@1.64.1 from npm with its bundled Chromium (156.0.8078.4, the version the 1.64 release notes list) and ran every script below on 23 October 2026. Real-site captures use https://playwright.dev. The rest use a local test page we wrote with a 64-pixel fixed header, a timestamp and a random price that change on every load, a spinning loader, a paragraph that fades in over 3 seconds, a bar that slides across over 4 seconds, a focused text input, a badge inside a shadow root, and enough height to scroll.
The basic call: viewport or full page
By default page.screenshot() captures the viewport. With fullPage: true it captures "the full scrollable page, instead of the currently visible viewport", in the words of Playwright's API reference. Leave out path and you get a Buffer back.
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage({ viewport: { width: 1280, height: 800 } });
await page.goto('https://playwright.dev/', { waitUntil: 'networkidle' });
for (const fullPage of [false, true]) {
const started = Date.now();
const image = await page.screenshot({ path: `out/playwright-dev-${fullPage ? 'full' : 'viewport'}.png`, fullPage });
const ms = Date.now() - started;
console.log(`fullPage: ${fullPage} ${image.readUInt32BE(16)} × ${image.readUInt32BE(20)} px, ${image.length} bytes, ${ms} ms`);
}
await browser.close();
})();
Output, first of two runs (the second gave the same sizes and 106 ms and 145 ms):
fullPage: false 1280 × 800 px, 343855 bytes, 99 ms
fullPage: true 1280 × 3016 px, 673443 bytes, 159 ms
The two numbers read from the buffer are the width and height stored in the PNG header, a quick check that needs no image library.
Trap 1: a fixed header lands where you scrolled
fullPage does not scroll back to the top first. We scrolled the test page down 1,000 pixels and then took a full-page capture:
await page.evaluate(() => window.scrollTo(0, 1000));
const image = await page.screenshot({ path: 'out/fullpage-scrolled.png', fullPage: true });
console.log(`${image.readUInt32BE(16)} × ${image.readUInt32BE(20)} px, scrollY after = ${await page.evaluate(() => scrollY)}`);
1280 × 2773 px, scrollY after = 1000
Scanning a column of the image for the header's colour found it at rows 1,000 to 1,063 and nowhere else. The top of the page has no header, and the middle of the page has one painted across the content:

This matters after any step that scrolls: clicking a button low on the page, a lazy-loading scroll pass, or a cookie banner you dismissed. Scroll back with window.scrollTo(0, 0) before the capture, or hide fixed elements with the style option shown below. Our Puppeteer full-page guide covers the other full-page failures, such as sections that only render when scrolled into view; they apply to Playwright in the same way.
Trap 2: clip below the fold needs fullPage
clip takes x, y, width and height and crops the image to that box. We read the bounding box of a card at the bottom of the test page and tried three clips:
const footer = await page.locator('#footer-card').boundingBox();
console.log('footer card box:', footer);
const tries = [
{ name: 'clip-top', clip: { x: 0, y: 0, width: 800, height: 300 } },
{ name: 'clip-below-viewport', clip: { x: 0, y: footer.y, width: 800, height: 120 } },
{ name: 'clip-below-fullpage', clip: { x: 0, y: footer.y, width: 800, height: 120 }, fullPage: true },
];
for (const { name, ...options } of tries) {
try {
const image = await page.screenshot({ path: `out/${name}.png`, ...options });
console.log(`${name}: ${image.readUInt32BE(16)} × ${image.readUInt32BE(20)} px`);
} catch (error) {
console.log(`${name}: ${error.message.split('\n')[0]}`);
}
}
footer card box: { x: 24, y: 2609.4375, width: 760, height: 115.8125 }
clip-top: 800 × 300 px
clip-below-viewport: page.screenshot: Clipped area is either empty or outside the resulting image
clip-below-fullpage: 800 × 120 px
Without fullPage, the clip is applied to the viewport image, so a box 2,609 pixels down is outside it and the call throws. With fullPage: true the same box returned the card. If what you want is one element, locator.screenshot() is simpler, because it finds the box for you.
One element with locator.screenshot()
locator.screenshot() captures "a screenshot of the page, clipped to the size and position of a particular element", according to the Locator reference. It waits for Playwright's actionability checks and scrolls the element into view first. The same reference warns that if the element is covered by other elements, they will be in the picture.
We scrolled the test page so the first card sat under the fixed header and took two captures of it:
await page.evaluate(() => window.scrollTo(0, 120));
const card = page.locator('#clock-card');
const plain = await card.screenshot({ path: 'out/element-plain.png' });
console.log(`element: ${plain.readUInt32BE(16)} × ${plain.readUInt32BE(20)} px, scrollY after = ${await page.evaluate(() => scrollY)}`);
await card.screenshot({
path: 'out/element-styled.png',
style: 'header { visibility: hidden; } .badge { display: none; }',
});
element: 760 × 238 px, scrollY after = 96
Playwright scrolled the page to 96, which put the card's top edge at the top of the viewport, right under the 64-pixel header. The first capture has the header over the card's heading. The second, with a style that hides the header, is clean:

Hiding things with style, including inside shadow DOM
The style option is CSS that Playwright applies only while it takes the screenshot. The API reference says this stylesheet "pierces the Shadow DOM and applies to the inner frames". We checked that against an ordinary stylesheet. The test page's discount badge lives in a shadow root with the class badge:
await page.addStyleTag({ content: '.badge { display: none; }' });
const box = await page.locator('promo-badge').boundingBox();
console.log('badge width after addStyleTag:', box.width);
badge width after addStyleTag: 134.921875
A normal style tag can't reach into the shadow root, so the badge stayed 135 pixels wide. Passing the same rule as style: '.badge { display: none; }' to locator('#form-card').screenshot() removed the badge from the image. This makes style the right place for rules that hide chat widgets, cookie bars and fixed headers, many of which are built as web components.
Getting the same image twice: animations and mask
Repeatable screenshots are what visual regression tests depend on. Two options exist for it:
animations: 'disabled' stops CSS animations, CSS transitions and Web Animations. Finite animations are fast-forwarded to completion; infinite ones are cancelled to their initial state and played again after the screenshot.
mask takes a list of locators and covers each one's bounding box with a solid box, pink #FF00FF unless you set maskColor.
We measured them together. The script loads the test page five times, waits a different time on each run (300 ms to 1,900 ms), captures, and counts how many different images it got:
const { chromium } = require('playwright');
const crypto = require('node:crypto');
const path = require('node:path');
const variants = {
defaults: () => ({}),
'animations disabled': () => ({ animations: 'disabled' }),
'animations disabled + mask': (page) => ({
animations: 'disabled',
mask: [page.locator('#clock'), page.locator('#price')],
maskColor: '#888888',
}),
'animations disabled + mask on fixed-width blocks': (page) => ({
animations: 'disabled',
mask: [page.locator('#clock-card p').first(), page.locator('#price')],
maskColor: '#888888',
}),
};
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage({ viewport: { width: 1280, height: 800 } });
for (const [name, options] of Object.entries(variants)) {
const hashes = new Set();
for (let run = 0; run < 5; run++) {
await page.goto('file://' + path.resolve('test-page.html'));
await page.waitForTimeout(300 + run * 400);
const image = await page.screenshot({ ...options(page), path: `out/repeat-${name.replaceAll(/\W+/g, '-')}-${run}.png` });
hashes.add(crypto.createHash('sha256').update(image).digest('hex'));
}
console.log(`${name}: ${hashes.size} different image(s) from 5 captures`);
}
await browser.close();
})();
Output of the first of four runs:
defaults: 5 different image(s) from 5 captures
animations disabled: 5 different image(s) from 5 captures
animations disabled + mask: 3 different image(s) from 5 captures
animations disabled + mask on fixed-width blocks: 1 different image(s) from 5 captures
The third line varied between 3 and 4 across the runs; the other lines were the same every time. To see what still moved, we compared the images pixel by pixel with Pillow:
| Options | Area that differed between captures | What it was |
|---|
| Defaults | x 43 to 477, y 197 to 586 | Timestamp, price, spinner, fading paragraph, sliding bar |
animations: 'disabled' | x 63 to 339, y 197 to 276 | Timestamp and price only |
| Plus a mask on the two text spans | x 333 to 342, y 195 to 213 | The right edge of the mask itself |
| Plus a mask on the whole paragraph and price line | none | Identical in all five captures |
The third row is the subtle one. A mask covers the element's bounding box, and an inline span holding a timestamp is as wide as its text. With a proportional font, 08:13:29.066 and 08:14:12.272 are not the same width, so the grey box was a few pixels wider or narrower on each run. Masking the block-level paragraph around it, whose width is set by the layout and not by the text, fixed it. The hero image at the top of this article shows a default capture next to the final one: the faint paragraph and the sliding bar have jumped to their end state, the spinner has gone back to its start, and the live values are under grey boxes.
The API reference adds one more detail: masks apply to invisible elements too, unless you narrow the locator to visible ones.
If you write tests with Playwright Test, expect(page).toHaveScreenshot() already defaults to animations: 'disabled', caret: 'hide' and scale: 'css', according to its type definitions, and accepts the same mask and a stylePath file in place of style.
Scale and format
On a high-density screen, scale: 'device' (the default) gives one image pixel per device pixel, and scale: 'css' gives one per CSS pixel. type picks PNG, JPEG or WebP. WebP arrived in Playwright 1.62; its default quality of 100 is lossless and lower values are lossy. JPEG defaults to quality 80. We captured playwright.dev in a 1280 × 800 viewport with deviceScaleFactor: 2:
const runs = [
{ scale: 'device', type: 'png' },
{ scale: 'css', type: 'png' },
{ scale: 'css', type: 'jpeg' },
{ scale: 'css', type: 'jpeg', quality: 90 },
{ scale: 'css', type: 'webp' },
{ scale: 'css', type: 'webp', quality: 80 },
];
for (const options of runs) {
const image = await page.screenshot({ ...options, animations: 'disabled' });
const { width, height } = await page.evaluate(async (b64) => {
const bitmap = await createImageBitmap(await (await fetch(`data:image/*;base64,${b64}`)).blob());
return { width: bitmap.width, height: bitmap.height };
}, image.toString('base64'));
console.log(JSON.stringify(options).padEnd(44), `${width} × ${height} px`, `${(image.length / 1024).toFixed(0)} KB`);
}
Output:
{"scale":"device","type":"png"} 2560 × 1600 px 880 KB
{"scale":"css","type":"png"} 1280 × 800 px 336 KB
{"scale":"css","type":"jpeg"} 1280 × 800 px 90 KB
{"scale":"css","type":"jpeg","quality":90} 1280 × 800 px 119 KB
{"scale":"css","type":"webp"} 1280 × 800 px 271 KB
{"scale":"css","type":"webp","quality":80} 1280 × 800 px 50 KB
The same results as a table:
| Options | Image size | File size |
|---|
scale: 'device', PNG | 2560 × 1600 px | 880 KB |
scale: 'css', PNG | 1280 × 800 px | 336 KB |
scale: 'css', JPEG, default quality | 1280 × 800 px | 90 KB |
scale: 'css', JPEG, quality 90 | 1280 × 800 px | 119 KB |
scale: 'css', WebP, default quality | 1280 × 800 px | 271 KB |
scale: 'css', WebP, quality 80 | 1280 × 800 px | 50 KB |
Three runs gave identical numbers. Lossless WebP saved about a fifth over PNG on this page, and lossy WebP at 80 was the smallest file. For how those formats compare across more pages and quality levels, see our PNG, JPEG and WebP measurements; for what device scale factor changes beyond the pixel count, see device pixel ratio in screenshots. Two smaller options round this out: omitBackground: true drops the default white background so transparent pages come out transparent (not for JPEG), and caret defaults to 'hide', so a focused input's blinking cursor doesn't make captures differ.
A checklist for clean Playwright screenshots
- Set the viewport size, and
deviceScaleFactor if you need sharp images, when you create the page.
- Wait for the content you need, not a fixed time.
- Scroll back to the top before a
fullPage capture if anything scrolled the page.
- Use
fullPage: true whenever a clip box sits below the first screen.
- Hide fixed headers, banners and widgets with
style, which also reaches shadow roots.
- For repeatable images, set
animations: 'disabled' and mask live values by a container with a fixed width.
- Pick
scale: 'css' and a format to control file size.