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

There are two different ways to “highlight” an element with Playwright: show a temporary overlay in the live browser while debugging, or save an image with the element visibly outlined. Use locator.highlight() for the first; for the second, add CSS during a page screenshot. A locator screenshot saves only the element itself, not a full page with the element marked.

Choose the kind of highlight you need

Your goal Use What you get
See which element a locator matches while debugging locator.highlight() A temporary visual overlay in the browser; it does not itself create an image file.
Save an image of just the matched element locator.screenshot() An image clipped to the element’s position and size.
Save a viewport or full page with the target outlined Apply screenshot-time CSS, then call page.screenshot() A page image with a visible CSS outline around the target.

For interactive locator inspection, Playwright UI Mode and Inspector can also show which page element a locator corresponds to. Use screenshot capture when you need a file to attach, compare, or publish.

Show a live debugging highlight

Choose a locator that identifies the intended element, then call highlight():

const button = page.getByRole('button', { name: 'Save' });
await button.highlight();

The example uses the button’s accessible role and name, which is usually clearer than an unqualified selector. The locator must resolve to the element you intend to inspect. Playwright describes highlight() as a debugging aid and cautions: “Useful for debugging, don’t commit the code that uses locator.highlight().” It is not a substitute for adding an annotation to an image artifact.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
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

The Locator API documents an optional style for changing the overlay’s appearance:

await locator.highlight({ style: 'outline: 2px dashed red' });

The documented style option was added in Playwright v1.60. Check the API reference matching the Playwright package installed in your project before relying on it. The API also documents hideHighlight() to hide a highlight previously added by highlight().

Save only the matched element

Use a locator screenshot when the image should contain the element alone:

await page.getByRole('button', { name: 'Save' }).screenshot({
  path: 'save-button.png',
});

This captures the element’s own bounding area, rather than a full browser viewport with an outline around the button. The locator screenshot waits for actionability checks and scrolls the element into view before capturing it. If the element detaches from the DOM, the operation throws.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Covered content: if another element overlays the target, the covered part will not become visible in the screenshot.
  • Scrollable content: when the target is inside a scrollable container, the screenshot includes the currently scrolled content; it does not reveal hidden portions outside that view.
  • Dynamic pages: if the target is replaced or removed while capture is underway, the locator screenshot can fail because the element is detached.

If you need a screenshot of the page surrounding the element, use a page screenshot instead.

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

Capture a page image with the target outlined

For a viewport or full-page artifact, add a CSS rule through the screenshot’s style option. Replace the sample selector with one that matches the element in your page:

await page.screenshot({
  path: 'highlighted-page.png',
  style: `
    [data-testid="save-button"] {
      outline: 3px solid red !important;
      outline-offset: 3px !important;
    }
  `,
});

The data-testid in this example is illustrative: it only works if your page actually has that attribute. You can use an appropriate selector for your application, including a test-specific attribute or a stable class. The screenshot-time stylesheet is applied while capturing; the Locator API says it pierces Shadow DOM and applies to inner frames. The documented screenshot style option was added in Playwright v1.41, so verify it against your installed version.

Capture the viewport or the full page

By default, page.screenshot() captures the page’s current viewport. Set fullPage: true to capture the full scrollable page:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.screenshot({
  path: 'highlighted-full-page.png',
  fullPage: true,
  style: `
    [data-testid="save-button"] {
      outline: 3px solid red !important;
      outline-offset: 3px !important;
    }
  `,
});

Use a viewport screenshot when the user’s visible screen is the point of the image; use a full-page capture when content below the fold must also appear. If the target is not present in the captured page area or does not match the stylesheet selector, no outline will appear.

Keep the annotation limited to the screenshot

Screenshot-time CSS is useful because the emphasis is applied as part of capture rather than requiring you to permanently change application styles. Make the selector specific enough to affect only the intended target, and use !important where the site’s own CSS might override the outline. An outline generally does not take up layout space; outline-offset separates it from the element edge.

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.

The screenshot API also provides mask and maskColor. These options cover matched elements with a colored box; they are useful for masking content, not for drawing a transparent outline around an element.

Choose a locator that identifies the right element

Playwright’s locator guidance recommends user-facing methods such as getByRole(), getByText(), getByLabel(), and getByPlaceholder(); getByTestId() is another option when test identifiers fit the project. Prefer a locator whose meaning is clear and which resolves to the intended target. A broad selector is not automatically unique.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const saveButton = page.getByRole('button', { name: 'Save' });
await saveButton.screenshot({ path: 'save-button.png' });

If a locator matches several similar elements, narrow it using the surrounding context or a project test ID rather than assuming the first match is correct. Playwright’s locator guide demonstrates filtering a list item before taking its screenshot, a useful pattern when an element’s context distinguishes it from similar items.

Find and inspect the locator interactively

If you are unsure which selector identifies the target, use Playwright’s inspection tools before adding a capture step.

  • Playwright UI Mode lets you inspect test runs and DOM snapshots; its locator picker highlights candidates as you hover and helps form a locator.
  • Playwright Inspector lets you edit a locator and see live highlighting in the browser while debugging tests.

These workflows help you identify the correct element. Once you have it, call locator.highlight() for a live debugging overlay or take a screenshot using the method that matches the image you need.

Rank #4
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

Return an image buffer for further processing

When you need to add an annotation after Playwright has rendered the page, capture a buffer instead of writing directly to a path. The screenshot guide demonstrates returning a screenshot buffer; you can pass that buffer to an image-processing step in your own project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const image = await page.screenshot({
  fullPage: true,
  style: `
    [data-testid="save-button"] {
      outline: 3px solid red !important;
      outline-offset: 3px !important;
    }
  `,
});
// Pass `image` to your chosen image-processing library.

This example already draws the outline during capture. A buffer is useful when the next step needs to modify or inspect the image in memory; the exact post-processing code depends on the library and the annotation you want. Playwright’s screenshot options also include settings for animation handling, caret behavior, image type, quality, scale, masks, and timeout. Check the installed version’s API reference for available options and their precise behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot missing or incorrect highlights

The live highlight does not identify the intended element

Check the locator in the Inspector or UI Mode and confirm that its role, accessible name, or test ID matches the target. If several elements qualify, narrow the locator using context. The live overlay is for debugging and does not save a screenshot.

The screenshot has no outline

  • Confirm that the CSS selector actually matches the page element. A sample test ID is not present automatically.
  • Confirm the screenshot call includes the style string and that the installed Playwright version supports that option.
  • Check for a competing page style; a specific selector and !important can help ensure the outline is visible.
  • Use a page screenshot for a page or viewport image. A locator screenshot is clipped to the target itself.

The element screenshot is incomplete

Check whether another element covers the target and whether it sits in a scrollable container. Covered portions remain covered, and only the container’s currently scrolled content is included. If the element is removed or replaced during capture, stabilize the page or locator target before retrying.

The screenshot option is rejected

Screenshot options are version-sensitive. In particular, the Locator API notes that the locator highlight style arrived in v1.60 and screenshot-time style arrived in v1.41. Compare your installed package version with the matching Playwright API reference rather than assuming every current documentation option exists in an older installation.

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.
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.

Or skip the browser setup

If you need a screenshot through an API instead of setting up a Playwright browser workflow, ScreenshotNeo takes a URL in one GET request and returns an image or PDF. The example below requests a WebP screenshot of Stripe; replace the URL and provide your API key. See the ScreenshotNeo API documentation for request options.

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

ScreenshotNeo removes cookie/consent banners, newsletter popups, and chat widgets before capture. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. Its MCP server gives AI agents tools to take screenshots, get page information, and capture PDFs. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. These are URL-based captures, so use Playwright when your task specifically requires its locator debugging, test integration, or screenshot-time styling behavior.

Try ScreenshotNeo with the ScreenshotNeo website, or sign up free for 1,000 screenshots a month with no card.

Frequently Asked Questions

Does locator.highlight() save a screenshot?

No. It adds a visual debugging overlay in the live browser. Use a screenshot method when you need an image file.

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

Can I use a locator screenshot to show the whole page around an element?

No. locator.screenshot() is clipped to the matched element. Use page.screenshot() for a viewport or full-page image.

Which Playwright versions support screenshot-time CSS?

The Locator API documents the screenshot style option as added in v1.41. Check the API reference for the version installed in your project.

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.