October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Capture a Website Screenshot with a Custom Viewport in Playwright

Set width and height before navigating in Playwright, then choose whether the size belongs to one page, a context, or a test project.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set the viewport’s pixel width and height before navigating, then call page.screenshot(). For a one-off page, use page.setViewportSize(); configure the browser context or Playwright Test project when the same size should apply more broadly.

Set a custom viewport for one page

In Playwright Library, call page.setViewportSize() with an object containing width and height, both in pixels. Set it before page.goto() so the site loads with the intended responsive viewport.

As an Amazon Associate I earn from qualifying purchases.

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  try {
    const page = await browser.newPage();
    await page.setViewportSize({ width: 1280, height: 720 });
    await page.goto('https://example.com');
    await page.screenshot({ path: 'screenshot.png' });
  } finally {
    await browser.close();
  }
})();

Save this as a JavaScript file in a project with Playwright installed, then run it with Node.js. It writes screenshot.png in the current directory. The API method is setViewportSize, not Puppeteer’s setViewport. Playwright’s Page API notes that changing viewport can reset the page’s screen size and that some websites do not expect a phone-sized viewport to change after loading. See the Page API reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose where to configure the viewport

Use page-level configuration for a single page, a browser context for pages sharing the same environment, or Playwright Test configuration for a test or project. Context configuration is also the right place to set viewport alongside screen and device-emulation options.

#1 Best Overall
Philips 24 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 241V8LB | 1920×1080 LED Monitor, 4ms GtG, Adaptive Sync, HDMI 1.4, Tilt Adjust, Low Blue Light, 4yr Advance Replacement Warranty
  • CRISP CLARITY: This 23.8″ Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
  • INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
  • THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors
  • WORK SEAMLESSLY: This sleek monitor is virtually bezel-free on three sides, so the screen looks even bigger for the viewer. This minimalistic design also allows for seamless multi-monitor setups that enhance your workflow and boost productivity
  • A BETTER READING EXPERIENCE: For busy office workers, EasyRead mode provides a more paper-like experience for when viewing lengthy documents

For every page in a browser context

const context = await browser.newContext({
  viewport: { width: 1280, height: 720 },
});
const page = await context.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'context-shot.png' });

For Playwright Test

import { test } from '@playwright/test';

test.use({ viewport: { width: 1280, height: 720 } });

test('capture at a custom viewport', async ({ page }) => {
  await page.goto('https://example.com');
  await page.screenshot({ path: 'screenshot.png' });
});

To set a default for a test project rather than an individual test, place viewport in that project’s use configuration in the Playwright configuration file. The emulation guide documents viewport and device settings.

For a device preset with a custom viewport

Spread the device descriptor first and then specify your viewport so your explicit dimensions override the descriptor’s viewport. Include deviceScaleFactor if the intended device pixel ratio matters as well.

Rank #2
Philips 22 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 221V8LB | 1920×1080 LED Monitor, 4ms GtG, Adaptive Sync, HDMI 1.4, Tilt Adjust, Low Blue Light, 4yr Advance Replacement Warranty
  • CRISP CLARITY: This 22 inch class (21.5″ viewable) Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
  • 100HZ FAST REFRESH RATE: 100Hz brings your favorite movies and video games to life. Stream, binge, and play effortlessly
  • SMOOTH ACTION WITH ADAPTIVE-SYNC: Adaptive-Sync technology ensures fluid action sequences and rapid response time. Every frame will be rendered smoothly with crystal clarity and without stutter
  • INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
  • THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors
const { chromium, devices } = require('playwright');

const browser = await chromium.launch();
const context = await browser.newContext({
  ...devices['iPhone 13'],
  viewport: { width: 390, height: 844 },
  deviceScaleFactor: 2,
});
const page = await context.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'mobile.png' });
await browser.close();

Use a device preset when you need its emulation settings, not just a convenient viewport size. A viewport and device pixel ratio are separate choices; consult the emulation guide for the configuration supported by your installed Playwright version.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose what the screenshot includes

By default, page.screenshot() captures the visible viewport. Choose the capture extent and output scale deliberately:

Rank #3
Sale
Dell 24 Monitor - SE2426H - 23.8-inch FHD (1920x1080) 144Hz 1ms Display, in-Plane Switching (IPS) Technology, AMD FreeSync™, TÜV 3-Star 2X HDMI, Tilt
  • Clear visuals. Fluid motion: A 144Hz refresh rate and 1ms MPRT deliver smooth, tear‑free motion across work, gaming, and streaming for clearer, more fluid viewing.
  • Eye comfort: TÜV Rheinland 3‑star* certification reduces harmful blue light while preserving stunning color quality without compromise. *TÜV Rheinland 3-star eye comfort certification.
  • Wide viewing angle: Get consistent views across a wide 178° /178° viewing angle.
  • In-Plane Switching (IPS): See excellent color accuracy and consistency across wide viewing angles with In-plane Switching (IPS) technology.
  • Ultra-thin bezels: Maximize your viewing experience with thin bezels.
  • Viewport: omit extent options to capture what is currently visible.
  • Full page: use fullPage: true to capture the scrollable document.
  • Rectangle: use clip with pixel coordinates and dimensions to capture a specific region.
  • CSS-pixel output: scale: 'css' produces one output pixel per CSS pixel.
  • Device-pixel output: scale: 'device' uses device pixels and can produce substantially larger high-DPI images.
await page.screenshot({ path: 'full-page.png', fullPage: true });
await page.screenshot({ path: 'css-pixels.png', scale: 'css' });
await page.screenshot({ path: 'device-pixels.png', scale: 'device' });
await page.screenshot({
  path: 'region.png',
  clip: { x: 0, y: 0, width: 640, height: 360 },
});

The screenshot API also supports file paths, PNG/JPEG/WebP output, JPEG or WebP quality options, and transparent backgrounds for supported formats. It can capture an element, too. For exact option behavior, check the Page API reference for your installed release. If you omit path, page.screenshot() returns an image buffer you can process in memory. The screenshot guide covers the basic capture workflow.

Make visual comparisons reproducible

A fixed viewport controls the page’s CSS layout width and height, but it does not guarantee identical pixels across machines. For useful visual comparisons, keep the operating system, browser version, browser settings, hardware conditions, and headless mode consistent. Playwright’s visual comparisons guidance explains why environment consistency matters.

Rank #4
Sale
Samsung 27" Essential S3 (S36GD) Series FHD 1800R Curved Computer Monitor | 100Hz, Game Mode, Advanced Eye Comfort, HDMI and D-sub Ports (LS27D366GANXZA)
  • CURVED FOR ENHANCED ENGAGEMENT: An immersive viewing experience with a curved monitor that wraps more closely around your field of vision; It creates a wider view, enhancing depth perception and minimizing peripheral distraction
  • SMOOTH PERFORMANCE FOR SEAMLESS CONTENT: Stay in the action when playing games, watching videos, or working on creative projects; The 100Hz refresh rate reduces lag and motion blur so you don't miss a thing in fast-paced moments¹
  • MORE GAMING POWER: Gain the edge with optimizable game settings; Color and image contrast can be adjusted to see scenes more vividly and spot enemies hiding in the dark; Game Mode adjusts any game to fill the screen so you can view every detail²
  • KEEP IT EASY ON THE EYES: Care for your eyes and stay comfortable, even during long sessions; Advanced eye comfort technology certified by TÜV reduces eye strain by minimizing blue light and reducing irritating screen flicker²
  • INCREASED VERSATILITY: Connect to more; Plug devices straight into your monitor for increased flexibility, making your computing environment even more convenient
  • Use the same viewport dimensions and device scale factor in each run.
  • Use the same browser and installed Playwright version.
  • Keep operating system, browser settings, and headless mode consistent.
  • Wait for the page content you care about before capturing; otherwise timing or incomplete rendering can change the image.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot viewport and screenshot problems

The page still looks like the old viewport

Set the viewport before navigation, rather than changing it after the page loads. If you need a shared size, create the context with viewport before making its pages.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The screenshot is smaller or larger than expected

Check whether you requested CSS pixels or device pixels. scale: 'device' can increase output dimensions on a high-DPI emulated device. Also distinguish the viewport-sized default from fullPage: true, which captures the full scrollable document.

Best Value
Sale
Sceptre New 22-Inch Gaming Monitor, FHD 1080p, Up to 144Hz, HDMI, DisplayPort, Built-in Speakers, Machine Black (E225W-FW144 Series, 2026)
  • 【INTEGRATED SPEAKERS】Whether you're at work or in the midst of an intense gaming session, our built-in speakers provide rich and seamless audio, all while keeping your desk clutter-free.
  • 【EASY ON THE EYES】 Protect your eyes and enhance your comfort with Blue-Light Shift technology. This feature reduces harmful blue light emissions from your screen, helping to alleviate eye strain during long hours of use and promoting healthier viewing habits.
  • 【WIDEN YOUR PERSPECTIVE】Our sleek minimal bezel design ensures undivided attention. The nearly bezel-free display seamlessly connects in a dual monitor arrangement, delivering an unobstructed view that lets you focus on more at once, completely distraction-free.

The image differs across machines

Matching viewport dimensions alone is not enough for pixel-for-pixel comparisons. Align browser version, operating system, settings, hardware conditions, and headless mode before treating differences as page changes.

The API method name does not work

For Playwright pages, use page.setViewportSize({ width, height }). The similarly named Puppeteer method is not the Playwright Page API method.

Or skip the browser setup

If you need a screenshot endpoint instead of managing a local Playwright browser, ScreenshotNeo returns an image or PDF from one GET request. Its compatible parameter names also make switching from other screenshot APIs easier. See the ScreenshotNeo API documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.