Use Selenium to run JavaScript that temporarily hides the page’s sticky header, take the screenshot, then restore the header’s original inline style in a finally block. The selector must match the target site; there is no universal Selenium selector for sticky headers.
Hide the header, capture the page, and restore its style
This Python example assumes the page’s header matches header.site-header. Inspect the page and replace that selector with one that identifies the intended header.
from selenium.webdriver.common.by import By
header = driver.find_element(By.CSS_SELECTOR, "header.site-header")
try:
original_style = driver.execute_script(
"const el = arguments[0]; "
"const old = el.getAttribute('style'); "
"el.style.setProperty('visibility', 'hidden', 'important'); "
"return old;",
header,
)
driver.save_screenshot("screenshot.png")
finally:
driver.execute_script(
"const el = arguments[0]; const old = arguments[1]; "
"if (old === null) el.removeAttribute('style'); "
"else el.setAttribute('style', old);",
header,
original_style,
)
Selenium’s execute_script runs JavaScript in the selected window or frame, while its screenshot method captures the current browsing context. The pattern above records the element’s previous inline style attribute before changing visibility, then restores it even if screenshot capture raises an exception. This is an implementation pattern based on Selenium’s documented operations, not a universal header-hiding API. See Selenium’s WebDriver documentation and the Python WebDriver API.
As an Amazon Associate I earn from qualifying purchases.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows 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 reinstallChoose the right selector and screenshot target
Use a page-specific selector
header.site-header is only an example. A broad selector such as header can match the wrong element on a page with multiple headers. Inspect the page’s markup and choose a selector that identifies the sticky header you want to hide.
Capture the viewport or an element
Use the current-context screenshot for the overall browser view. Use Selenium’s element screenshot operation when the desired artifact should contain only one element. Selenium documents both screenshot targets; the correct choice depends on what the image needs to show. See Selenium’s screenshot documentation.
Why this method uses visibility
The example applies visibility: hidden with important priority, which hides the header without removing its layout space. It then restores the original inline style attribute—or removes the attribute if none existed before. Whether display: none is preferable depends on the page layout; it changes layout space, and there is no general rule established here that it is better for sticky headers.
Troubleshooting
- The header is still visible: confirm the selector matched the visible sticky element, and that the script ran in the window or frame containing it. Selenium executes scripts in the selected browsing context.
- The wrong header disappears: replace a broad selector with a more specific selector for the target page.
- The page looks different after capture: keep the restoration in
finallyand verify that it runs after the screenshot operation. - The screenshot does not show the expected result: check the output in the browser and driver combination used by your project. This pattern has not been verified against every combination.
Or skip the browser setup
ScreenshotNeo can capture a URL with one request, without setting up Selenium. Its API accepts the parameter names used by other screenshot APIs, which can make switching easier. See the ScreenshotNeo API documentation.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
Rank #4
Rank #3
Rank #2
#1 Best Overall
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
Before capture, ScreenshotNeo accepts the cookie or consent banner like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and billing status. Its 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; paid plans start at $5 for 3,000 shots.
Sign up for ScreenshotNeo’s free plan.
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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.




