How to save and load cookies in Selenium?

When web scraping, we often need to save the connection state like browser cookies and resume it later. Using Selenium, to save and load cookies we can use driver.get_cookies() and driver.add_cookie() methods:

import json
from pathlib import Path
from selenium import webdriver

driver = webdriver.Chrome()

# Get cookies to a json file:
    json.dumps(driver.get_cookies(), indent=2)

# retrieve cookies from a json file
for cookie in json.loads(Path('cookies.json').read_text()):

Question tagged: Selenium, Headless Browsers, Python

Related Posts

How to Scrape With Headless Firefox

Discover how to use headless Firefox with Selenium, Playwright, and Puppeteer for web scraping, including practical examples for each library.

Selenium Wire Tutorial: Intercept Background Requests

In this guide, we'll explore web scraping with Selenium Wire. We'll define what it is, how to install it, and how to use it to inspect and manipulate background requests.

Web Scraping Dynamic Web Pages With Scrapy Selenium

Learn how to scrape dynamic web pages with Scrapy Selenium. You will also learn how to use Scrapy Selenium for common scraping use cases, such as waiting for elements, clicking buttons and scrolling.