Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › automated testing

How to Take a Selenium Screenshot of a Page Behind a Login

By Freedom251 Team · · 5 min read

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

To capture a page behind a login with Selenium, establish an authenticated browser session, open the protected page, wait for a reliable sign that its content is ready, and save the current window with driver.get_screenshot_as_file(). For repeatable tests where login itself is not under test, Selenium recommends setting up application state outside the browser flow where possible—for example, using an authorized API or fixture to obtain a session and then adding its cookie.

Choose how to authenticate

First decide whether the test is checking the login flow or only needs an authenticated starting point. That choice determines whether to automate the login page or set up the session directly.

As an Amazon Associate I earn from qualifying purchases.

Approach Use it when Trade-off
Automate the login form The login behavior itself is part of the test. Exercises the browser login flow, but repeats that setup whenever the test runs.
Use an authorized API or test fixture, then set browser state The test concerns a protected page and login is only setup. Can avoid repeated UI login preparation; the application must provide a permitted way to establish the session.

Selenium’s testing guidance describes gaining access to the application under test with an API login and cookie setup, and recommends avoiding repeated browser-login preparation when possible: Generating application state. If you are testing authentication itself, automate the form instead. Selenium’s 2021 article discusses form-based, HTTP Basic and Digest authentication in the context of Selenium 4; treat it as historical context, not a universal recipe for every current identity provider: A Tour of 4: Authentication.

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

Set up a logged-in session with a cookie

The example below assumes your authorized test setup has already obtained a valid session cookie. Replace the URL, cookie name and value, readiness selector, and output path with values for your application. It first visits the cookie’s domain, adds the cookie, opens the protected route, waits for an application-specific element, and writes a PNG.

#1 Best Overall
CloudValley Webcam Cover for Logitech C920x / C920 / C922x / C922 / C930e | C920x HD Pro Web Cam Compatible, Camera Cover Protects Lens & Security, Protect Your Visual Privacy, Black
  • Privacy Protection and Lens Care: Avoid private information from hacking while preventing dust-fall and scratching of the camera lens
  • Multiple Compatibility: Suitable for Logitech webcam C920x, C920, C922, C930e, C922x Pro Stream HD Camera
  • Artful Design: Modeled and designed exclusively to fit the above devices from Logitech and make it more stylish
  • Easy Flip Mechanism: Can be turned 180 angle and easily take the cover off when flipping more than 180
  • Simple Installation: Attaches securely to your Logitech webcam without leaving residue, allowing for quick and hassle-free setup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

# Obtain this through your application's permitted test API or fixture.
session_value = "REPLACE_WITH_TEST_SESSION_VALUE"

driver = webdriver.Chrome()
try:
    # WebDriver can add a cookie only in a browsing context on its valid domain.
    driver.get("https://example.test/")
    driver.add_cookie({"name": "session", "value": session_value})

    driver.get("https://example.test/account")
    WebDriverWait(driver, 10).until(
        lambda d: d.find_element(By.CSS_SELECTOR, "[data-testid='account-page']")
    )

    saved = driver.get_screenshot_as_file("/tmp/account.png")
    if not saved:
        raise OSError("Screenshot could not be saved")
finally:
    driver.quit()

Install Selenium and configure a compatible browser and WebDriver for your environment before running the script. The code uses the standard Selenium Python WebDriver API; it does not assume a particular application’s authentication mechanism or prove that a cookie alone will unlock that application.

Cookie rules that matter

WebDriver cookie addition applies to the current browsing context. Navigate to a page on the cookie’s valid domain before calling add_cookie(); Selenium documents this requirement in Working with cookies. Set any domain, path, expiry, or other cookie attributes only as required by the application’s permitted test setup.

Rank #2
CloudValley Laptop Camera Cover Slide, Metal 0.023 Inch Ultra-Thin, 2 Packs | Web Webcam Cover for MacBook Pro / Air, Laptop, iMac, iPad, iPhone 8/7, Computer Accessories, Privacy Protection, Black
  • Privacy Protection: CloudValley webcam cover is designed for those who prioritize privacy, security, and peace of mind when using laptops, tablets, and computers
  • Fashion Design: The space aluminum alloy webcam cover features a subtle design which compliments the beautiful aesthetic of top devices
  • Ultra-Thin Design: Measures only 0.023 (0.6 mm) inch thin, ensuring it does not interfere with closing your laptop or device while providing reliable camera coverage
  • Broad Compatibility: Works flawlessly with most laptops (MacBook, HP, Dell, Asus, Acer, Lenovo), All-in-One PCs and leading tablets including iPad, Surface Pro, Galaxy Tab, Fire HD, and Google Pixel Tablet
  • Simple to Use: Only need to align to the webcam, attach and press it firmly for 15 seconds. Does not interfere with web use or indicator light

A session cookie is not a universal login shortcut. An application may need additional cookies, server-side state, a CSRF token, matching cookie attributes, or a different authentication mechanism. Use the application’s own test interface as the authority for establishing an accepted session. Treat session values as secrets: do not commit them or print them to logs.

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

Wait for the protected page, then capture it

A successful navigation is not necessarily proof that the authenticated content is ready. The example waits until a page-specific element appears. Choose a selector that exists on the intended authenticated page, rather than relying on an arbitrary sleep. Selenium’s API reference documents WebDriverWait separately from screenshot capture; the readiness condition should reflect your application.

Rank #3
Sale
Yilador Webcam Cover 3 Pack, 0.03 inch Ultra Thin Laptop Camera Cover Slide | Camera Blocker Slider for iPhone iPad MacBook Pro Computer iMac Cell Phone PC Laptop Accessories, Great for Privacy, Black
  • Note: Not suitable for MacBooks released after 2023 or devices with a protruding front camera; Not applicable to full-screen or notch-style tempered glass screen protectors; Do not use on the rear camera of the phone.
  • 💻 Why Do You Need a Webcam Cover Slide? — Safeguard your privacy by covering your webcam with our reliable webcam cover when not in use. Don't let anyone secretly watch you. Stay protected!
  • ✅ Thin & Stylish — Enhance your laptop's functionality and aesthetics with our 0.027" ultra-thin webcam covers. Seamlessly close your laptop while adding a touch of sophistication.
  • ✅ Fits Most Devices — Compatible with laptops, phones, tablets, desktops! Keep your privacy intact on Ap/ple, Mac/Book, iPh/one, iP/ad, H/P, L/novo, De/ll, Ac/er, As/us, Sa/msung devices.
  • ✅ 365 Days Protection — Our upgraded 3.0 adhesive ensures a strong hold that won't damage your equipment. Experience reliable, long-term privacy protection day in and day out.

Python’s get_screenshot_as_file(filename) saves the current window as a PNG. It returns True on success and False for an I/O error, so check the return value and use a writable output path ending in .png: Python WebDriver common API, Selenium 4.50.0.

Choose current-window or full-document capture

The Python method above captures the current window. If you need the whole document rather than the visible window, check support in the browser-specific driver you use. Selenium’s Firefox API documents get_full_page_screenshot_as_png() for full-document capture; do not assume that Firefox method is portable to every browser and driver combination: Firefox WebDriver API, Selenium 4.50.0.

Rank #4
JCWINY Webcam Cover, 2 Pack Desktop Computer External Webcam Lens Covers Shutter Cap Hood, Streaming Web Camera Privacy Cover Clip Compatible with Logitech HD Pro Web Cam C270/C615/C920/C930e/C922X
  • 【Premium Webcam Cover】This webcam privacy cover is an accessory of computer webcam. No worry about interfering with web camera lens use or indicator light; No damage to your device in any way as well. A helpful privacy protector and dust separator
  • 【Privacy Protector】Slide the web camera cover over your webcam lens when not in use, and prevents web hackers from Spying on you. It is perfect to provide privacy security and peace of mind to individuals, groups, organizations, companies and governments. It also protects your camera lens from dust, and keeps it in high-definition resolution all the ways
  • 【Durable Material】The web cam cover is made of high-strength plastic, which ensures that your privacy is protected for a long and lasting period of time. The back of the web camera privacy cover slide also has a strong 3M adhesive layer. It helps the privacy protector stick firmly to your device. The most convenient, super thin design, and extra mini size, make it perfectly combine with your devices
  • 【Wide Compatibility】This webcam cover is compatible with most popular webcams with flat area surrounding lens or with protruding lens, such as Logitech HD Pro Webcam C920 C920x C930e and C922, Logitech C615 and C270 (NOT fit Logitech C910, B910, C310). It can be also used as a cover for the peep hole on door
  • 【For Logitech Webcam Cover】 The streamcam cover kit comes with 2 pack. Please clean the lens surface before applying. Make sure the mounting surface is cleaned completely so that it sticks properly and firmly

Troubleshoot missing or incorrect screenshots

  • The screenshot shows the login page: Check that the test authentication was accepted, that the cookie was added after navigating to its valid domain, and that the protected URL and readiness selector match the expected application state.
  • The wait times out: Confirm the protected route loaded and that the selector exists on the authenticated page. A selector for content that is absent, renamed, or only present in a different state will never satisfy the wait.
  • add_cookie() fails: Navigate to a URL on the cookie’s valid domain first, and verify the cookie details against the application’s test setup.
  • No PNG appears: Check that the destination directory exists and is writable, that the filename ends in .png, and that get_screenshot_as_file() returned True.
  • The image cuts off the page: The standard method captures the current window. Use a full-document method only if the target browser’s driver supports one.
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 screenshot rather than a Selenium test, ScreenshotNeo provides a website screenshot API and MCP server. A single request can return an image or PDF; for example, this cURL request saves a WebP screenshot of a public URL. See the ScreenshotNeo API documentation for options and response details.

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

ScreenshotNeo accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; these steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, with response headers identifying the page verdict and billing status. Its MCP server provides screenshot and page-inspection tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000.

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

Best Value
Laptop Camera Cover Slide, 6 Pack Ultra-Thin 0.022in Webcam Cover Blocker | Web Camera Cover for Laptops,Computer and MacBook,iMac,iPad,iPhone.Essential Computer Accessories,Protect Your Web Privacy
  • 【Protect Privacy Security】Focusing on network security, now we can easily and effectively protect personal and family privacy security , Just gently slide the slide and close the camera, you can stop the intrusion of hackers.
  • 【 Ultra Thin Design】The new ultra-thin design, with a thickness of only 0.022 inches, is made of flexible ABS material and is not fragile. Will not affect the closing of the laptops and scratch the laptops.
  • 【Easy to install】 Strong adhesive makes the cover not fall, keep the screen clean and free of stains during installation, tear off the adhesive tape on the back, align it with our camera, and press hard for 10 seconds to work.
  • 【Compatible with 】Compatible with camera for Laptop, tablet, computers, Echo Show and Apple Devices,as: MacBook Pro,Macbook Air,iMac ,Mac mini,iPad,MacBook Air, iPhone 6/7/8 Plus etc front camera .
  • [What you get] 6 pack black webcam covers.

Frequently Asked Questions

Does adding a Selenium cookie always log me in?

No. A cookie works only if it represents valid application state and is added with the attributes and domain the application expects. Some systems require additional state or another authentication method.

Can Selenium save a full-page screenshot in every browser?

Do not assume so. The Firefox WebDriver API documents a full-document screenshot method; verify equivalent support for your specific browser and driver.

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

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.

automated testingPythonscreenshotsSeleniumWebDriver

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.

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair 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.