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 Google Maps

We'll take a look at to find businesses through Google Maps search system and how to scrape their details using either Selenium, Playwright or ScrapFly's javascript rendering feature - all of that in Python.

Web Scraping with Selenium and Python Tutorial + Example Project

Introduction to web scraping dynamic javascript powered websites and web apps using Selenium browser automation library and Python.

How to Scrape Dynamic Websites Using Headless Web Browsers

Introduction to using web automation tools such as Puppeteer, Playwright, Selenium and ScrapFly to render dynamic websites for web scraping