DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober 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 Now×
Skip to content
World desk3 min

How to Hide a Sticky Header Before Taking a Selenium Screenshot

Temporarily hide a sticky header with Selenium, capture the page or an element, then restore the header’s original inline style.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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

Choose 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 finally and 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.Support on Ko-Fi

Leave a Reply

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

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

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.