🚀 We are hiring! See open positions

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:

python
import json
from pathlib import Path
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")

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

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

driver.quit()
Scale Your Web Scraping
Anti-bot bypass, browser rendering, and rotating proxies — all in one API. Start with 1,000 free credits.
No credit card required 1,000 free API credits Anti-bot bypass included
Not ready? Get our newsletter instead.