Frontend Development 4 min read

How to Retrieve Browser Cookies Using Python: Selenium, Requests, and HAR Parsing

This article explains several Python techniques for extracting browser cookies—including Selenium automation, direct HTTP requests, driver‑based retrieval, and HAR file parsing—providing clear code examples and guidance on when to use each method.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
How to Retrieve Browser Cookies Using Python: Selenium, Requests, and HAR Parsing

When testing or developing web applications, obtaining browser cookies is essential because they store session state, user preferences, and other data. This guide presents four practical Python approaches for retrieving cookies, each with complete code samples.

1. Retrieve Cookies with Selenium

Use Selenium WebDriver to launch a browser, navigate to a page, and call get_cookies() to obtain all cookies.

from selenium import webdriver
# Initialize the browser driver
driver = webdriver.Chrome()
# Open a webpage
driver.get("http://example.com")
# Get browser cookies
cookies = driver.get_cookies()
# Print cookies
for cookie in cookies:
    print(cookie)
# Close the browser
driver.quit()

2. Retrieve Cookies with the Requests Library

Send an HTTP request directly with requests.get() and access the cookies attribute of the response.

import requests
# Send HTTP request
response = requests.get("http://example.com")
# Get response cookies
cookies = response.cookies
# Print cookies
for cookie in cookies:
    print(cookie.name, cookie.value)

3. Retrieve Specific Cookie Using Selenium Driver

After loading a page with Selenium, you can fetch all cookies or query a particular cookie by name.

from selenium import webdriver
# Initialize the browser driver
driver = webdriver.Chrome()
# Open a webpage
driver.get("http://example.com")
# Get all cookies
all_cookies = driver.get_cookies()
# Get a specific cookie by name
cookie_value = driver.get_cookie("cookie_name")
# Print all cookies
for cookie in all_cookies:
    print(cookie)
# Close the browser
driver.quit()

4. Parse Cookies from a HAR File

Export network traffic as a HAR file from the browser’s developer tools, then parse the JSON to extract cookie information.

import json
# Read HAR file
with open("example.har", "r") as file:
    har_data = json.load(file)
# Extract cookies from the first entry
cookies = har_data["log"]["entries"][0]["response"]["cookies"]
# Print cookies
for cookie in cookies:
    print(cookie["name"], cookie["value"]))

Each method suits different scenarios: Selenium is ideal for dynamic pages requiring JavaScript execution, Requests works for simple HTTP calls, driver‑based retrieval offers fine‑grained control, and HAR parsing is useful when you already have captured network logs.

Pythonweb scrapingbrowser-automationSeleniumcookies
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.