🚀 We are hiring! See open positions

How to save and load cookies in Playwright?

by scrapecrow Dec 05, 2022

When web scraping, we might want to pause our scraping session by saving cookies and resume it later. Using Playwright, to save and load cookies we need to refer to the context object which has methods cookies() and add_cookies():

import json
from pathlib import Path

from playwright.sync_api import sync_playwright

with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False)

    # To save cookies to a file first extract them from the browser context:
    context = browser.new_context(viewport={"width": 1920, "height": 1080})
    page = context.new_page()
    page.goto("https://httpbin.dev/cookies/set/mycookie/myvalue")
    cookies = context.cookies()
    Path("cookies.json").write_text(json.dumps(cookies))

    # Then, we can restore cookies from file:
    context = browser.new_context(viewport={"width": 1920, "height": 1080})
    context.add_cookies(json.loads(Path("cookies.json").read_text()))
    page = context.new_page()
    page.goto("https://httpbin.dev/cookies")
    print(context.cookies())  # we can test whether they were set correctly
    # will print:
    [
        {
            "sameSite": "Lax",
            "name": "mycookie",
            "value": "myvalue",
            "domain": "httpbin.dev",
            "path": "/",
            "expires": -1,
            "httpOnly": False,
            "secure": False,
        }
    ]

Related Articles

How to Scrape Facebook: Marketplace and Events

Complete guide to scraping Facebook data including Marketplace listings and Events. Covers authentication, anti-bot bypass, and production-ready techniques.

PYTHON
PLAYWRIGHT
SCRAPEGUIDE
How to Scrape Facebook: Marketplace and Events

Bypass Proxy Detection with Browser Fingerprint Impersonation

Stop proxy blocks with browser fingerprint impersonation using this guide for Playwright, Selenium, curl-impersonate & Scrapfly

PROXIES
SELENIUM
PLAYWRIGHT
PUPPETEER
BLOCKING
Bypass Proxy Detection with Browser Fingerprint Impersonation

Playwright Examples for Web Scraping and Automation

Learn Playwright with Python and JavaScript examples for automating browsers like Chromium, WebKit, and Firefox.

PLAYWRIGHT
PYTHON
NODEJS
Playwright Examples for Web Scraping and Automation

Web Scraping with Playwright and JavaScript

Learn about Playwright - a browser automation toolkit for server side Javascript like NodeJS, Deno or Bun.

PLAYWRIGHT
HEADLESS-BROWSER
NODEJS
Web Scraping with Playwright and JavaScript

Playwright vs Selenium

Explore the key differences between Playwright vs Selenium in terms of performance, web scraping, and automation testing for modern web applications.

HEADLESS-BROWSER
PLAYWRIGHT
SELENIUM
Playwright vs Selenium

What is a Headless Browser? Top 5 Headless Browser Tools

Quick overview of new emerging tech of browser automation - what exactly are these tools and how are they used in web scraping?

HEADLESS-BROWSER
PLAYWRIGHT
SELENIUM
PUPPETEER
What is a Headless Browser? Top 5 Headless Browser Tools