To hide a page element in a screenshot, target it with a CSS selector and apply display: none !important before or during capture. For a one-off image, use Chrome DevTools; for repeatable screenshots, inject a stylesheet in Playwright. If you want a colored cover rather than disappearance, Playwright’s mask option is a separate choice.
Find a selector for the element
- Open the page in Chrome, right-click the unwanted element, and choose Inspect. Alternatively, open DevTools’ element picker and click the element.
- In the Elements panel, check that the highlighted markup corresponds to the exact banner, popup, or region you want to hide. Prefer a meaningful ID or class shown in the markup when available; selectors based on position can be brittle.
- Use that selector in your CSS rule. For example, if the element has the class
cookie-banner, the selector is.cookie-banner. The example class is illustrative; pages do not necessarily use it.
Hide an element for a one-off screenshot in DevTools
With the element selected in the Elements panel, apply a temporary rule such as display: none !important; in the Styles pane, or add a rule using the selector you identified:
As an Amazon Associate I earn from qualifying purchases.
.cookie-banner { display: none !important; }
Replace .cookie-banner with the selector that matches the target on the page. Capture the screenshot while the rule is active. This changes the page’s appearance in your DevTools session; it does not save an edit to the website’s source code.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteHide an element in a Playwright screenshot
For scripted captures, pass a stylesheet string to the Page screenshot API’s style option. This option was added in Playwright v1.41. The current documentation says screenshot-time styles can hide dynamic elements, pierce Shadow DOM, and apply to inner frames. See Playwright Page API documentation.
await page.screenshot({
path: 'page.png',
style: '.cookie-banner { display: none !important; }'
});
Use the selector you verified in the rendered page. If it matches nothing, the injected CSS has nothing to hide. Check the resulting image rather than assuming the rule worked.
#1 Best Overall
Reuse a stylesheet in screenshot tests
For visual assertions, keep the rules in a CSS file and pass its path as stylePath to toHaveScreenshot:
await expect(page).toHaveScreenshot({ stylePath: './screenshot.css' });
For example, screenshot.css could contain:
iframe { display: none !important; }
Playwright documents stylePath as a way to filter dynamic or volatile content to make screenshot comparisons more deterministic. See Playwright test snapshots documentation.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Rank #2
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Choose between hiding and masking
| Method | What appears in the image | Use it when |
|---|---|---|
CSS hiding, such as display: none |
The element is removed from the visible composition. | You want the screenshot to show the page without that element. |
Playwright mask |
A colored overlay covers the bounds of matched locators. The documented default is pink; a custom mask color is available. | You want a visible cover over the matched area rather than removing it from the composition. |
Masking is a visual cover, not removal or secure redaction. Neither screenshot-time hiding nor masking establishes that underlying page data has been deleted or comprehensively redacted. See Playwright’s screenshot API documentation for the masking option.
Common problems and fixes
- The element still appears: Reinspect it and verify the selector matches the rendered element. Check spelling, punctuation, and whether you targeted a child instead of the surrounding banner.
- The rule works in DevTools but not in a script: Make sure the same selector is passed in the screenshot call’s
stylestring or included in the file referenced bystylePath. - The layout has an unexpected gap: Confirm that the target selector matches the entire unwanted region. Hiding only an inner child may leave its parent or surrounding layout in place.
- A screenshot test still varies: Identify the dynamic element causing the difference and add a narrowly targeted rule to the shared stylesheet. Avoid hiding unrelated content, which can conceal meaningful regressions.
- You expected a colored block:
display: nonemakes the element disappear. Use Playwright’smaskoption when a colored overlay is the intended result.
Or skip the browser setup
ScreenshotNeo can capture a page through one API request and supports CSS selectors for capturing a specific element, as well as custom CSS and JavaScript. Its default cleanup accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each cleanup step can be turned off. Only clean shots are billed: bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, with the outcome reported in response headers. It also has an MCP server with screenshot tools for AI agents. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots.
Example cURL request, using the documented API parameters:
Rank #3
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
For API options and setup details, see the ScreenshotNeo documentation. To hide a particular element, configure its CSS selector with the API’s selector-related capture options in your request.
Sign up for ScreenshotNeo to get 1,000 screenshots a month free, with no card required.
Quick Recap
Best Value
Rank #4
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
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.




