October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober 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 Screenshot of a Shadow DOM Element in Playwright

Use Playwright’s locator.screenshot() to capture a target inside an open shadow root, with guidance on selectors, output options, and troubleshooting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a Playwright locator for the element inside the component’s open shadow root, then call locator.screenshot(). For example, page.locator('my-widget .target') can match a target in an open shadow root because Playwright CSS selectors pierce open roots.

Capture an element inside an open shadow root

In a Playwright test or script, locate the element and take its screenshot directly:

As an Amazon Associate I earn from qualifying purchases.

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

test('capture a Shadow DOM element', async ({ page }) => {
  await page.goto('https://example.com');

  const target = page.locator('my-widget .target');
  await target.screenshot({ path: 'element.png' });
});

Replace my-widget and .target with the custom element and target selector on the page. The URL is illustrative; use the page you need to capture. The locator screenshot API takes a screenshot of the element matching the locator and returns image data; path saves it to that file. See the Playwright Locator API.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Choose a locator that can reach the target

CSS selectors

Playwright CSS selectors pierce open shadow roots, so a selector can cross a custom-element boundary, as in my-widget .target. This is useful when the component structure is the intended target, but selectors tied to internal class names or markup may need updating when that implementation changes.

#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

Role and text locators

When the element has a reliable accessible role or visible text, prefer a user-facing locator where it identifies the target unambiguously. This can make a test less dependent on component internals. Consult Playwright’s locator guidance for supported locator strategies.

XPath and closed roots

XPath selectors do not pierce shadow roots. The documented CSS behavior applies to open shadow roots; it does not establish a general locator method for reaching elements inside a closed shadow root. If a selector does not find the element, check whether the root is open and whether the target actually exists in the rendered page.

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

Control what the element screenshot contains

Waits, scrolling, and visibility

Before taking the screenshot, Playwright performs actionability checks and scrolls the element into view. If the element is detached while the screenshot is being taken, the operation errors. An overlay that covers the target remains relevant: covered portions are not made visible by the screenshot. For an element in a scrollable container, the image reflects the container’s current scroll position rather than automatically capturing all of its scrollable contents.

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

Disable motion and mask changing regions

For more repeatable captures, disable animations and mask dynamic or sensitive areas:

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.
await page.locator('my-widget .target').screenshot({
  path: 'element.png',
  animations: 'disabled',
  mask: [page.locator('my-widget .live-value')],
});

The animations option disables CSS animations, transitions, and Web Animations according to the documented behavior. Use mask when a changing region should not affect the output. Ensure each mask locator identifies the intended region.

Choose image type and scale

The screenshot options let you select type as PNG, JPEG, or WebP, and set scale to CSS-pixel or device-pixel output. Choose the format and scale according to how the image will be stored or used. Check the Locator API documentation for the option details supported by your installed Playwright release.

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

Apply screenshot-only styles

The locator screenshot style option was added in Playwright v1.41, according to the API documentation. It applies a stylesheet that pierces Shadow DOM and inner frames, which can help hide or adjust dynamic content for a controlled capture. If you use this option, confirm that your installed version supports it and scope CSS carefully so it changes only the intended capture.

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

When the capture fails or looks wrong

  • No element matches: verify the custom-element name and target selector against the rendered page. Confirm the shadow root is open; CSS piercing does not make closed roots generally accessible, and XPath will not cross a shadow boundary.
  • The screenshot call errors because the element detached: the page may have replaced or removed the node between locating it and capture. Wait for the component to finish rendering, then locate the current element and screenshot it.
  • The target is missing or partly covered in the image: check overlays, sticky controls, and other elements that can obscure it. The screenshot records what is visible; it does not reveal covered pixels.
  • The image shows only part of a long component: check whether the target is inside a scrollable container and whether that container is at the desired scroll position. Element screenshots do not automatically substitute a full-page or full-scroll-container capture.
  • The output changes between runs: disable animations and mask volatile areas where appropriate. For page-specific styling, use the screenshot style option if supported by the Playwright version in use.
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 website screenshot rather than a Playwright test of a particular DOM node, ScreenshotNeo can return a screenshot or PDF with one GET request. It is a website screenshot API and MCP server; it does not replace a locator screenshot when you need to target a specific element inside a page.

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.

With the URL parameter names used by other screenshot APIs, a basic cURL request is:

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

See the ScreenshotNeo API documentation for request options. Cookie banners, newsletter popups, and chat widgets are removed before the shot; each step can be turned off. Bot checks, blank pages, and failed loads are never billed. An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The Free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo free: 1,000 screenshots a month, no card required.

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

Frequently Asked Questions

Does Playwright CSS pierce every Shadow DOM?

The documented selector behavior covers open shadow roots. It does not establish a general locator solution for closed roots.

Can I use XPath to select an element inside a shadow root?

No. Playwright’s XPath selectors do not pierce shadow roots; use a supported locator strategy such as CSS for an open root.

What does locator.screenshot() return?

It returns image data for the matched element. Supplying the path option also saves the screenshot to a file.

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.

Leave a Reply

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

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.

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
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.