Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Handle Cookies in Selenium WebDriver

Navigate to the cookie’s domain first, then use Selenium’s binding-specific methods to add, inspect, or delete cookies. Python and Java examples included.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To handle cookies in Selenium WebDriver, first navigate to a page on the cookie’s domain, then use your language binding’s add, get, or delete cookie methods. In Python, for example, use driver.add_cookie() to add one, driver.get_cookies() to inspect all cookies, and driver.delete_cookie() or driver.delete_all_cookies() to remove them.

Why you must open the cookie’s domain first

WebDriver cookie commands apply to the current browsing context. Navigate to a page on the domain for which the cookie is valid before adding or inspecting it. Selenium’s guide states: “First of all, you need to be on the domain that the cookie will be valid for.” Selenium: Working with cookies

As an Amazon Associate I earn from qualifying purchases.

If the homepage is slow, Selenium notes that you can open a smaller page on the same site before adding the cookie. The important point is that the current page must be on the relevant domain.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add, inspect, and delete cookies in Python

The following Python examples use the cookie methods shown in Selenium’s documentation. Check the API reference for the Selenium version and browser binding installed in your project; signatures and accepted fields can vary across language bindings.

Add a cookie

After navigating to the relevant domain, pass a cookie dictionary to add_cookie():

driver.get("https://example.com/")
driver.add_cookie({"name": "key", "value": "value"})

The required example fields are the cookie’s name and value. Add attributes only when your test needs them and the binding accepts them.

Get one cookie or inspect all cookies

Use get_cookie() to retrieve a cookie by name, or get_cookies() to inspect the cookies available in the current context:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cookie = driver.get_cookie("foo")
cookies = driver.get_cookies()

Use the all-cookies method when you need to examine the current cookie set rather than look up a single known name.

Delete one cookie or clear the current set

Delete a named cookie with delete_cookie(). Use delete_all_cookies() only when the test intends to clear every cookie in the current context:

driver.delete_cookie("test1")
driver.delete_all_cookies()

Cookie attributes beyond name and value

Selenium’s documentation includes cookie fields and builder options such as path, secure, and sameSite. Its Python examples demonstrate sameSite values of Strict and Lax. Consult the reference for your language binding and version for the exact accepted keys and syntax.

For example, the documentation shows the attribute shape as a dictionary alongside the name and value:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
driver.add_cookie({
    "name": "key",
    "value": "value",
    "path": "/",
    "secure": True,
    "sameSite": "Lax"
})

Choose attributes to match the site and the test case; do not assume that every binding exposes identical method signatures or accepts identical cookie fields.

Equivalent Java API placement

In Java, cookie operations are exposed through the driver’s management interface. Selenium’s examples include adding a cookie with a Cookie object and retrieving the set with getCookies():

driver.manage().addCookie(new Cookie("key", "value"));
Set<Cookie> cookies = driver.manage().getCookies();

As with Python, navigate to the relevant domain first and check the Java API reference for the Selenium version used by the project. Java WebDriver.Options API reference

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common cookie-handling problems

  • Adding a cookie before navigating: First open a page on the cookie’s valid domain, then call the add method.
  • Looking for a cookie that is not in the current context: Confirm the driver is on the relevant site, then inspect the set with the binding’s get-all method.
  • Using attribute syntax from a different language: Follow the official example or API reference for the binding and version in your project; method placement and accepted fields differ.
  • Clearing more than the test intended: Use the named-cookie delete method when removing one cookie. Reserve delete-all for tests that intentionally clear the entire current cookie set.
  • Assuming an optional attribute is universally supported: Check the installed binding’s reference and the browser in use before relying on fields such as sameSite or secure.

Or skip the browser setup

If your goal is a clean screenshot rather than exercising WebDriver cookie behavior, ScreenshotNeo can capture a page without setting up a browser script. Its API accepts a URL in one GET request; the examples below use https://stripe.com.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Python:

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

See the ScreenshotNeo API documentation for request details. Before capture, it accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, with response headers indicating the page verdict and billing status. An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Sign up for ScreenshotNeo free.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.