ScreenshotNeo

BlogHow-to

How to Reuse Firefox Cookies for Selenium Screenshots of a Logged-In Website

Transfer authorized Firefox cookies into Selenium, verify the logged-in session, and capture a page screenshot. Includes runnable Python, cURL, and Node.js examples.

By the ScreenshotNeo team4 October 20269 min read

Short answer: Selenium can reuse selected Firefox cookies if you transfer their names, values, and relevant attributes into WebDriver’s cookie API. First navigate Firefox WebDriver to the cookie’s domain, add the cookies, then open the authenticated page and confirm the site recognizes the session before taking the screenshot. Firefox stores profile cookies in cookies.sqlite, but Selenium does not document a direct import from that database. Use an authorized export method and treat every cookie value as a secret.

1. What this workflow does—and its limits

A Firefox profile may contain cookies that represent login status. Mozilla identifies cookies.sqlite as the profile’s cookie store, and lists DOM storage separately. Copying cookies therefore may restore a session, but it does not migrate the entire browser state; an application may also depend on DOM storage or other state. Mozilla: profiles and stored user data.

The supported Selenium operation is to add cookie objects to the current browser session. Selenium’s cookie guide says: “First of all, you need to be on the domain that the cookie will be valid for.” Visit a matching host before calling add_cookie. Selenium WebDriver cookies.

This guide assumes you are authorized to access the account and automate the target site. Do not copy session cookies from someone else’s profile or share, commit, log, or publish cookie values: they can grant account access.

2. Export only the relevant cookies

Use a trusted, authorized method to obtain the cookies for the target site as structured data. Firefox’s cookie database is an implementation store, not a documented Selenium interchange format. The source documentation does not define a universal database-to-WebDriver importer, so the exact extraction method depends on your environment and Firefox version.

Prepare a list of dictionaries with at least name and value. Preserve attributes that apply to the cookie:

  • domain and path control where it is sent.
  • secure means it is intended for secure connections.
  • httpOnly indicates script access restrictions; preserve the field when the export supplies it.
  • expiry is the expiration time as a Unix timestamp in seconds.
  • sameSite may be Strict, Lax, or None, depending on the browser and cookie.

WebDriver requires the cookie’s name and value; the other attributes are optional. A cookie for a different document domain is rejected. See the MDN Add Cookie command reference and Selenium Firefox WebDriver API.

3. Runnable Python example: inject cookies and capture a screenshot

Install Selenium with python -m pip install selenium. Selenium Manager can arrange a compatible driver in supported setups; otherwise install and configure the Firefox driver as required by your environment. Replace the example cookie entries with values exported for an account and site you are authorized to use. Keep this file out of source control.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# Replace these examples with authorized cookie data. Do not commit real values.
cookies = [
    {
        "name": "sessionid",
        "value": "REPLACE_WITH_SECRET",
        "domain": "example.com",
        "path": "/",
        "secure": True,
        "httpOnly": True,
        # "expiry": 1790000000,
        # "sameSite": "Lax",
    }
]

start_url = "https://example.com/"
target_url = "https://example.com/account"

options = Options()
# Uncomment for a headless run:
# options.add_argument("-headless")

driver = webdriver.Firefox(options=options)
try:
    # Selenium requires the current page to be on the cookie's domain.
    driver.get(start_url)

    for cookie in cookies:
        # Omit absent/unknown optional attributes. WebDriver accepts
        # name and value plus supported cookie fields.
        driver.add_cookie(cookie)

    driver.get(target_url)

    # Add an application-specific check here: for example, wait for an
    # account-only element or assert that the login link is absent.
    print("Current URL:", driver.current_url)
    print("Page title:", driver.title)

    # Viewport screenshot. For a full-document screenshot, use the
    # Firefox-specific full-page API supported by your Selenium version.
    driver.save_screenshot("logged-in.png")
finally:
    driver.quit()

The screenshot above captures the current viewport. Selenium’s Firefox API also documents full-page capture with get_full_page_screenshot_as_file (or the corresponding bytes method). For example, replace the save line with driver.get_full_page_screenshot_as_file("logged-in-full.png") when the installed Firefox WebDriver exposes that method. Firefox WebDriver screenshot methods.

Check that authentication actually worked

A successful add_cookie call only means WebDriver accepted the cookie. Before capture, check a known authenticated condition: an account page heading, a user menu, a profile link, or an application endpoint whose response indicates an authenticated session. Prefer an explicit wait for that condition over an arbitrary sleep. If the site redirects to login or shows an access-denied page, do not treat the screenshot as a successful logged-in capture.

Field Use Common mistake
name, value Required cookie identifier and contents. Using a placeholder or truncated value instead of the complete exported value.
domain Cookie host/domain scope, if provided. Trying to add a cookie for a host different from the page currently loaded.
path Path scope, often /. Omitting a non-root path when the site relies on it.
secure Preserves secure-connection restriction. Testing a secure cookie over plain HTTP.
httpOnly Preserves the cookie’s script-access property. Assuming this flag prevents WebDriver from setting the cookie.
expiry Expiration Unix time in seconds. Passing milliseconds or an already expired timestamp.
sameSite Same-site policy where supported. Using an unsupported spelling or value for the WebDriver/browser version.

When possible, preserve the exported attributes rather than guessing. If a cookie has no domain attribute, it may be host-only; avoid broadening its scope by inventing a domain. Selenium’s add-cookie command is constrained by the current browsing context’s domain, and browser/WebDriver versions can differ in accepted serialization details.

Cookie injection is useful when you want to transfer a selected set of cookies into a fresh WebDriver session. A persistent profile can retain broader browser state, but the reviewed Selenium documentation does not provide a version-specific recipe for reusing a Firefox profile, so profile setup and lifecycle need to follow the documentation for your installed Selenium and Firefox versions.

Choose injection when the required authentication is represented by a manageable set of cookies and you can export them safely. Consider a profile-based workflow when the site depends on broader state such as DOM storage, but validate the profile approach carefully and avoid running two Firefox processes against the same profile at once. Neither approach guarantees login: the site may expire, rotate, bind, or otherwise reject session credentials.

6. cURL, Python, and Node.js with ScreenshotNeo

If you need a screenshot of a publicly accessible page and do not need to reuse your Firefox login, ScreenshotNeo provides a website screenshot API and MCP server. It does not take your local Firefox cookies through the one-call examples below; use Selenium for a session that requires your browser’s authenticated state. ScreenshotNeo’s API documentation covers its request options.

cURL

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

Python

import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://example.com"},
    timeout=90,
)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)

Node.js

const q = new URLSearchParams({
  access_key: 'YOUR_API_KEY',
  url: 'https://example.com'
});
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
const bytes = new Uint8Array(await res.arrayBuffer());
await import('node:fs/promises').then(fs => fs.writeFile('shot.webp', bytes));

7. Or skip the browser setup

For pages that do not require your Firefox login, ScreenshotNeo can return an image or PDF from one GET request. Its capture options include full-page screenshots with lazy images loaded, CSS selector element capture, device and viewport settings, custom CSS and JavaScript, wait conditions, and more. Cookie banners, popups, and chat widgets are removed before the shot. Bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots.

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

Create a free ScreenshotNeo account for 1,000 screenshots a month with no card.

8. Troubleshooting

Symptom Likely cause Fix
InvalidCookieDomainException or Add Cookie fails The current page is on another host, or the cookie domain does not match the current document. Navigate to the exact site host first. Check whether the cookie is host-only or scoped to a parent domain; do not invent a wider domain.
Cookie is accepted but the site still shows login The cookie expired, was rotated, belongs to another environment, or the site needs state outside cookies. Export a fresh authorized session cookie, inspect expiry and attributes, and check whether the site uses DOM storage or other session state.
Cookie works on one route but not another Its path or domain scope excludes the target route or subdomain. Preserve the original path/domain attributes and navigate to the matching host and path.
Secure cookie is not sent The target was opened over HTTP, or a proxy/environment changes the scheme. Use the site’s HTTPS URL and verify the final URL after redirects.
Login succeeds but screenshot shows a loading or incomplete page The capture happened before app rendering or lazy content completed. Wait for a stable, authenticated page element and any required content condition before capturing.
Firefox fails to start or WebDriver reports a compatibility error Browser/driver setup is missing or incompatible. Use Selenium Manager where supported, or configure a driver compatible with the installed Firefox and Selenium versions.
Full-page method is missing The installed Firefox WebDriver API/version differs. Check the Firefox WebDriver API for the installed version; use viewport capture with save_screenshot if needed.
Screenshot contains a login page despite an apparently valid cookie The site may bind a session to additional state, reject automation, or redirect after checking access. Inspect the final URL and page condition. Use the authorized browser state required by the site; cookie injection cannot guarantee an authenticated result.

9. Performance, reliability, and cost

Cookie injection itself adds little work compared with browser startup and loading the target page. Reusing a running driver for multiple pages can avoid repeated startup, but isolate accounts and sessions and always quit the driver when finished. Waiting for a specific ready condition is usually more reliable than choosing a fixed delay; a fixed delay can be too short on a slow page and waste time on a fast one.

Reliability depends on session lifetime, cookie scope, site behavior, browser state, and network access. Treat authentication as a condition to verify for each capture, especially if cookies can rotate or expire. A failed authenticated capture should be detected before downstream processing.

Local Selenium costs include the machine and time needed to run Firefox; there is no universal runtime or cost figure for this workflow. ScreenshotNeo charges only for clean shots: cache hits, bot checks/CAPTCHAs, blank pages, timeouts, and failed loads cost nothing, with X-Page-Verdict and X-Billed response headers reporting the outcome. Plans are Free: 1,000/month; Starter: $5 for 3,000; Growth: $15 for 15,000; Pro: $39 for 60,000; Scale: $99 for 250,000; Business: $249 for 1,000,000. Yearly billing gives two months free, and every feature is on every plan.

10. FAQ

Can I point Selenium at Firefox’s cookies.sqlite file?

The reviewed documentation does not describe a direct database import. Extract selected cookies using an authorized method and add them through WebDriver’s cookie API, preserving the needed attributes.

No. Transfer only cookies relevant to the target site and required flow. Keeping the set minimal reduces exposure of unrelated session credentials.

Can I use ScreenshotNeo for a page that needs my Firefox login?

The one-call ScreenshotNeo examples do not import a local Firefox profile or its cookies. Use Selenium when the target requires that authenticated browser session.

Does save_screenshot capture the full page?

It captures the current viewport. Firefox’s WebDriver API provides a separate full-page screenshot method; availability depends on the installed API version.