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

Use Playwright’s locator.screenshot() on a locator that reaches the element inside an open shadow root. Playwright CSS selectors can cross open shadow roots; XPath cannot. For example: await page.locator('my-widget .target').screenshot({ path: 'element.png' });

Capture an element inside an open Shadow DOM

In Playwright, an element locator can take a screenshot of the matched element rather than the whole page. CSS selectors pierce open shadow roots, so a selector can start at the custom element and continue to a descendant inside its shadow tree.

  1. Locate the custom element and its target using a selector that matches the page’s actual markup.
  2. Call screenshot(), optionally setting a file path and capture options.
  3. Run the script and inspect the saved image to confirm the target and visible content are what you expect.
import { chromium } from 'playwright';

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

try {
  await page.goto('https://example.com');
  await page.locator('my-widget .target').screenshot({
    path: 'element.png',
    animations: 'disabled',
  });
} finally {
  await browser.close();
}

my-widget .target is an illustrative selector, not a selector guaranteed to exist on a particular site. Replace it with the custom element and descendant that identify your target. The selector guidance covers open shadow roots; it does not establish a general locator method for reaching elements in closed shadow roots.

Choose a locator that can reach the target

CSS selectors

A CSS locator such as my-widget .target can pierce an open shadow root. CSS is useful when the component structure is the intended way to identify the element, but selectors tied to internal class names or markup may need updating when that implementation changes. Playwright’s locator guidance documents Shadow DOM selector behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Philips 24 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 241V8LB
  • 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

Role and text locators

When a user-facing role or text identifies the target reliably, prefer that locator strategy for maintainability. Choose the locator based on what the page exposes and what your test intends to identify; do not assume a role or text locator will uniquely identify every element inside a component.

XPath and closed roots

XPath selectors do not pierce shadow roots. The cited guidance specifically describes CSS piercing open roots; it does not establish that Playwright can use the same approach to reach content in a closed root.

Rank #2
Philips 22 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 221V8LB
  • 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

What element screenshots include

locator.screenshot() captures the page area corresponding to the matched element and returns a buffer. Set path to save the result to a file. Before capture, Playwright performs actionability checks and scrolls the element into view. If the element detaches during the operation, the call errors. These behaviors and options are documented in the Locator API.

  • Occlusion: If another element covers the target, the covered portion will not actually be visible in the screenshot.
  • Scrollable content: A screenshot of an element in a scrollable container shows the currently scrolled content, not unseen portions of that container.
  • Output: The screenshot method returns a buffer; use path when you want Playwright to write the image to a file.

Control the screenshot output

Use screenshot options to make captures more repeatable or suitable for the task. The exact available options should be checked against the Playwright version installed in your project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
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.
  • animations: 'disabled' disables CSS animations, transitions, and Web Animations under the documented behavior.
  • mask covers dynamic or sensitive regions in the capture.
  • type selects PNG, JPEG, or WebP output.
  • scale selects CSS-pixel or device-pixel output.
  • style applies a stylesheet for the capture. The option was added in Playwright v1.41 according to the documentation; its stylesheet can pierce Shadow DOM and inner frames, which can help hide or alter dynamic content.

For example, to mask a dynamic region, pass a locator in the mask option and use a file path if you need a saved artifact. Check the installed release’s API documentation for the option’s exact type and syntax before adding it to a larger capture workflow. The current-next documentation also describes the API; compare it with the version you run rather than assuming a next-version option is already available: Locator API, Next.

Troubleshoot missing or failed captures

  • The locator does not match: Confirm the custom element and descendant selector match the actual page. Check that the root is open; the documented CSS piercing behavior applies to open Shadow DOM.
  • The element is found but part is missing: Check whether another element overlaps it and whether a scrollable ancestor is showing the content you intend to capture.
  • The screenshot call errors because the element detached: The target may have been replaced during rendering. Wait for the page or component to reach a stable state, then locate and capture the current element.
  • XPath fails to reach the target: Use a supported locator strategy such as CSS for an open shadow root, or a reliable user-facing role/text locator where suitable.
  • The output changes from run to run: Disable animations and consider masking dynamic regions. If the installed Playwright release supports the screenshot style option, use it to apply capture-specific CSS.
  • The captured image has unexpected dimensions or format: Set scale and type explicitly, and consult the Locator API for the installed release.

Or skip the browser setup

If you need a website screenshot through an API rather than a Playwright locator, ScreenshotNeo takes a screenshot from one GET request. Its API captures a page URL, not a selector inside a page’s Shadow DOM, so use Playwright above when you specifically need one matched shadow-tree element.

Rank #4
Sale
Samsung 27" Essential S3 (S36GD) Series FHD 1800R Curved Computer Monitor
  • 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

Example request and options are documented at ScreenshotNeo’s API docs:

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

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 step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and the response includes X-Page-Verdict and X-Billed headers. An 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.

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

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does Playwright’s CSS selector pierce every Shadow DOM?

The documented behavior cited here covers open shadow roots. It does not establish a general locator method for closed roots.

Which Playwright method captures just one matched element?

Use locator.screenshot(); it captures the matched element’s page area and returns a buffer.

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.

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