Save and load cookies in Playwright with Python
You can reuse a logged-in browsing session across separate Playwright runs by exporting cookies from one browser context and importing them into a new one.
Export with context.cookies(), store the result as JSON, then restore it with context.add_cookies() before opening the page that needs the session.
Full example
The complete pattern uses two separate sync_playwright() blocks. The first launches a browser, logs in, and writes cookies.json. The second launches a new browser and context, reads the file, restores the cookies, and then opens the authenticated page. Because the second context is entirely new, this demonstrates that the saved file alone carries the session state. See protected module code0003 for the full example.
Install Playwright and Chromium, then set LOGIN_URL and DASHBOARD_URL for an account and site you are authorized to use. This independent example pauses for you to complete sign-in, saves cookies.json, closes the first browser and restores those cookies in a fresh context. Cookies contain session secrets: keep the file private. Cookie restoration alone does not guarantee authentication when a site also needs other storage or an expired session must be renewed.
import json
import os
from pathlib import Path
from playwright.sync_api import sync_playwright
cookie_file = Path("cookies.json")
with sync_playwright() as engine:
browser = engine.chromium.launch(headless=False)
try:
context = browser.new_context()
page = context.new_page()
page.goto(os.environ["LOGIN_URL"])
input("Complete sign-in in the browser, then press Enter here: ")
cookie_file.write_text(json.dumps(context.cookies()), encoding="utf-8")
finally:
browser.close()
with sync_playwright() as engine:
browser = engine.chromium.launch(headless=False)
try:
context = browser.new_context()
context.add_cookies(json.loads(cookie_file.read_text(encoding="utf-8")))
page = context.new_page()
page.goto(os.environ["DASHBOARD_URL"])
input("Inspect the restored session, then press Enter to close: ")
finally:
browser.close()Common errors and limitations
Missing url or domain/path: each cookie passed to add_cookies() needs either url, or both domain and path. Cookies obtained from cookies() already satisfy this; manually assembled dictionaries can fail if those fields are omitted.
Cookies do not apply retroactively: adding them after a page loads will not resend that request. Restore them before page.goto() when the first load must be authenticated.
Expired cookies do not revive a session: expired entries can be loaded without an error, but the site will not treat them as a valid login. If a restored session is rejected, repeat the login and save a fresh cookies.json.
Cookies are not web storage: add_cookies() restores cookies only. To also preserve localStorage and sessionStorage, save with context.storage_state(path="state.json") and restore with browser.new_context(storage_state="state.json").
Quick decision checklist
Use cookies() plus add_cookies() when cookies alone are enough and you want a small JSON file. Use storage_state when the site also depends on localStorage or sessionStorage. In both cases, restore state into a new context before navigating, and re-save after a fresh login if the previous session has expired.