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 Screenshot a Specific Element in Playwright

Use Playwright’s locator.screenshot() to save an element-sized image or handle the returned buffer in memory. Learn the options and edge cases.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a Playwright locator’s screenshot() method: await page.locator('.header').screenshot({ path: 'element.png' }); Playwright scrolls the matched element into view, checks that it is actionable, and saves an image clipped to its bounds. You can also omit path and use the returned buffer in memory.

Capture an element with a locator

Here is a runnable Node.js example using Playwright’s asynchronous API. Replace the URL and selector with the page and element you need:

As an Amazon Associate I earn from qualifying purchases.

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

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  try {
    await page.goto('https://example.com');
    await page.locator('.header').screenshot({ path: 'element.png' });
  } finally {
    await browser.close();
  }
})();

The filename extension determines the saved image format. Playwright supports PNG, JPEG, and WebP; PNG is the documented default when no type is specified. See the official Locator API and Screenshots guide.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Choose a locator that identifies the right element

A CSS selector works when it is specific and stable, such as .header. For interfaces with meaningful accessibility labels, a role-based locator can be clearer:

await page.getByRole('link', { name: 'Pricing' }).screenshot({ path: 'pricing-link.png' });

Locators are resolved when the screenshot action runs, so they can work with elements rendered after navigation. If the locator matches no element or the target disappears before capture, the operation cannot produce the requested image.

Save a file or use the image buffer

Providing path writes the screenshot to disk. Without it, locator.screenshot() returns a buffer, which is useful when you want to upload, inspect, or otherwise process the image without first saving a local file:

const image = await page.locator('.header').screenshot();
// image is a Buffer

The returned buffer contains the image bytes. Choose the output type with the type option when needed; supported values are png, jpeg, and webp. When saving to a path, use a matching extension to make the intended format clear.

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

Control animation and pixel scale

For repeatable visual captures, disable motion and select an output scale explicitly:

await page.locator('.header').screenshot({
  path: 'header.png',
  animations: 'disabled',
  scale: 'css'
});
  • animations: 'disabled' disables CSS animations, transitions, and Web Animations for the capture. Finite animations are fast-forwarded to completion, firing transitionend; infinite animations are canceled to their initial state for the screenshot and then resume afterward.
  • scale: 'css' produces one output pixel per CSS pixel. scale: 'device' uses device pixels and can create a larger image on high-DPI displays. The documented default is device.
  • style accepts CSS to apply during the screenshot. It can hide changing elements or otherwise make captures more consistent; the injected style pierces Shadow DOM and applies to inner frames.

Option defaults can differ by Playwright release or language binding. Check the Locator API documentation for the version installed in your project.

Understand what the element screenshot includes

The result is clipped to the matched element’s bounds, not a screenshot of the entire page. Playwright scrolls the element into view and performs actionability checks first. Content covered by another element remains covered in the image; this method does not reveal obscured content.

For a scrollable target, the image shows the portion currently scrolled into view. It does not capture every item in the element’s internal scroll area as one tall image. If the element is detached from the DOM during capture, Playwright throws an error.

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

Troubleshoot common capture failures

  • The locator does not resolve: Check that the selector or accessible name matches the live page, and wait for the page’s rendering or the target element before calling screenshot().
  • The element disappears or is detached: A rerender may replace it between locating and capturing. Stabilize the page state and locate the element again immediately before the screenshot.
  • The image shows only part of a scrollable element: This is expected: locator screenshots capture the currently visible portion of a scroll container, not all of its scroll contents.
  • The target looks covered: Other page elements remain visible over it. A locator screenshot clips to bounds; it does not remove overlays or change stacking order automatically.
  • The output is larger or smaller than expected: Set scale to 'css' for CSS-pixel dimensions or 'device' for device-pixel dimensions.
  • The screenshot varies between runs: Disable animations with animations: 'disabled' and use the style option to suppress known dynamic content.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need a screenshot from a URL rather than an element within a Playwright-controlled page, ScreenshotNeo provides a one-request screenshot API. This request saves a WebP image:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. ScreenshotNeo accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents using Claude, Cursor, or another MCP client. 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: 1,000 screenshots a month, no card required.

Use the locator API, not the legacy element-handle method

Playwright marks ElementHandle.screenshot() as discouraged and recommends locator-based locator.screenshot() instead. Locators make the target identification part of the action and are the documented approach in the Screenshots guide. The locator screenshot method was added in Playwright v1.14; consult documentation matching your installed version if compatibility matters.

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

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
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.