Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk6 min

How to Capture a Screenshot of a Web Page with Basic Authentication in Selenium

Use Selenium to authenticate to an HTTP Basic Authentication page, wait for the protected content, and save a PNG—with browser-specific caveats and working Python code.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Authenticate the browser first, wait until the protected page is ready, then save a screenshot with Selenium. In Python, driver.save_screenshot("screenshot.png") captures the current window as a PNG and returns whether saving succeeded. HTTP Basic Authentication is not the same as filling in a website’s HTML login form; the method for supplying credentials depends on your browser and whether Selenium is running locally or through a hosted provider.

Set up Selenium and identify the protected page

This example uses Python and Chrome. Install Selenium with python -m pip install selenium, provide a URL that includes its scheme (https:// or http://), and replace the example address and readiness selector with values for your site.

As an Amazon Associate I earn from qualifying purchases.

For local browser automation, current Selenium releases can manage compatible browser drivers through Selenium Manager. If your environment requires a manually configured driver or uses a remote WebDriver server, follow that environment’s setup instead.

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.

Authenticate before capturing

HTTP Basic Authentication is handled by the browser’s HTTP authentication flow, rather than by a username and password form in the page. One possible approach for initial navigation is to put credentials in the URL, as BrowserStack documents for some supported browser and platform combinations: https://username:[email protected]/protected. This is not a universal WebDriver feature. Browser/version support varies, special characters such as @ and : may require URL encoding, and BrowserStack documents platform limitations, including some Safari on macOS and Android combinations. Check the BrowserStack compatibility guidance for your execution environment.

Putting credentials in a URL also creates a risk of accidental exposure through logs, debugging output, browser history, or shared test artifacts. Use a dedicated test account, keep secrets outside source control, and do not print or publish a credential-bearing URL. If this approach is unsupported or unsuitable, use an authentication mechanism supported by your browser or execution provider.

Runnable Python example using URL credentials

The example illustrates the URL approach for an environment that supports it. It reads secrets from environment variables, URL-encodes credential components, waits for an element that should appear only after successful authentication, checks the screenshot result, and always closes the browser.

import os
from urllib.parse import quote

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

username = os.environ["TEST_USERNAME"]
password = os.environ["TEST_PASSWORD"]
protected_url = "https://example.test/protected"

# URL-encode each credential component before inserting it into the URL.
# Do not print this URL: it contains secrets.
encoded_username = quote(username, safe="")
encoded_password = quote(password, safe="")
authenticated_url = protected_url.replace(
    "https://", f"https://{encoded_username}:{encoded_password}@", 1
)

driver = webdriver.Chrome()
try:
    driver.get(authenticated_url)
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "main"))
    )
    if not driver.save_screenshot("screenshot.png"):
        raise RuntimeError("Screenshot could not be saved")
finally:
    driver.quit()

Set TEST_USERNAME and TEST_PASSWORD in your shell or secret manager before running the script. Replace main with a meaningful element that demonstrates the protected content—not merely a generic page shell—has loaded. The 10-second wait is an example timeout, not a guarantee that every application will be ready within that interval.

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

Wait for the right page state, then save the image

Successful navigation alone does not prove that the browser authenticated or that the page finished rendering. Wait for an application-specific element or state that confirms the content you intend to capture is present. Selenium’s documentation does not prescribe one readiness condition for every site; choose one that fits the page, such as a report heading or a protected account panel. If the wait times out, inspect whether authentication failed, the selector is wrong, or the application needs a different readiness condition.

driver.save_screenshot("screenshot.png") saves the current window to a PNG file. Check its Boolean return value, as in the example, and save the file somewhere your test process can access. Selenium describes the WebDriver screenshot endpoint as returning a Base64-encoded screenshot; its Python convenience method handles saving the image to the path you provide. See the Selenium screenshot documentation and the Python WebDriver API.

Current window is not the full document

The generic screenshot method captures the current browsing context; do not assume it captures an entire long page. Selenium’s Python Firefox driver API documents get_full_page_screenshot_as_file and save_full_page_screenshot for full-document PNG screenshots. These are Firefox-specific documented methods, not a guarantee of equivalent behavior through every driver. Consult the Firefox WebDriver API and verify support in the driver you use.

Choose an authentication route for your execution environment

Situation Possible route Important qualification
Local Selenium, first navigation Try URL-embedded credentials only if your browser supports the pattern. Browser compatibility varies; credentials in a URL can leak through logs or artifacts.
BrowserStack Automate, first navigation BrowserStack documents the URL-credential approach. Follow its browser and platform compatibility notes.
BrowserStack Automate, later navigation BrowserStack documents its sendBasicAuth JavaScript executor. This is BrowserStack-specific, not a generic Selenium WebDriver command.
Other hosted or remote Selenium service Use an authentication route documented by that provider, if available. Do not assume BrowserStack’s executor works in another service or locally.

BrowserStack describes both its URL-based route and its provider-specific executor in its Basic HTTP Authentication guide. Verify the method against the exact browser, platform, and provider you run; the available documentation does not establish one approach that works identically everywhere.

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

Troubleshoot common failures

  • The page shows an authentication prompt or unauthorized response: The URL pattern may not be supported by that browser/provider, or the credentials may be incorrect. Verify the credentials and choose an authentication mechanism documented for the target environment.
  • The URL breaks when credentials contain punctuation: Encode the username and password separately before inserting them. Keep the resulting URL out of logs and reports.
  • The wait times out: Authentication may not have succeeded, the selector may not match the protected content, or the page may need a different readiness signal. Inspect the page state without exposing secrets, then use a reliable element or condition for that application.
  • The screenshot file is missing or empty: Check the returned value from save_screenshot, confirm the process can write to the chosen path, and inspect the test runner’s working directory.
  • The image cuts off lower content: The generic method is a current-window screenshot, not a promise of full-document capture. Use a documented full-page method supported by your driver, such as the Firefox Python methods, or adapt the capture approach to your environment.
  • sendBasicAuth is unavailable: It is a BrowserStack-specific executor documented for BrowserStack Automate. It is not part of generic Selenium WebDriver; use the relevant provider’s documented mechanism.
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 an image or PDF without managing Selenium authentication and browser setup, ScreenshotNeo is a website screenshot API and MCP server. Its capture process accepts consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before taking the shot; each step can be turned off. It bills only clean shots: bot checks, CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, with the response identifying the page verdict and billing status in headers. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents and MCP clients.

For HTTP Basic Authentication, pass credentials using the API’s supported request parameters; do not put secrets into public client-side code. See the ScreenshotNeo API documentation for authentication and request options.

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

ScreenshotNeo’s Free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account to try it.

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.

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.

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.