SeleniumHQ/selenium · warning · ValueError
Cookie name cannot be empty
Error message
Cookie name cannot be empty
What it means
Raised by `get_cookie` when `name` is falsy (empty string) or is all whitespace (`name.isspace()`). The guard rejects these before issuing the GET_COOKIE command, because an empty name is ambiguous and some backends misbehave. It is a ValueError; the docstring documents this contract.
Source
Thrown at py/selenium/webdriver/remote/webdriver.py:754
A list of dictionaries, corresponding to cookies visible in the
current session.
"""
return self.execute(Command.GET_ALL_COOKIES)["value"]
def get_cookie(self, name) -> dict | None:
"""Get a single cookie by name (case-sensitive,).
Returns:
A cookie dictionary or None if not found.
Raises:
ValueError if the name is empty or whitespace.
Example:
`cookie = driver.get_cookie("my_cookie")`
"""
if not name or name.isspace():
raise ValueError("Cookie name cannot be empty")
with contextlib.suppress(NoSuchCookieException):
return self.execute(Command.GET_COOKIE, {"name": name})["value"]
return None
def delete_cookie(self, name) -> None:
"""Delete a single cookie with the given name (case-sensitive).
Raises:
ValueError if the name is empty or whitespace.
Example:
`driver.delete_cookie("my_cookie")`
"""
# Firefox deletes all cookies when "" is passed as name
if not name or name.isspace():
raise ValueError("Cookie name cannot be empty")View on GitHub (pinned to aa36b38e69)
Solutions
- Validate the name is non-empty and not whitespace before calling get_cookie.
- Default to None semantics: if name is blank, skip the lookup and return None yourself.
- Strip and assert the name in a helper used by all cookie calls.
Example fix
# before c = driver.get_cookie(name) # name may be '' # after name = (name or '').strip() cookie = driver.get_cookie(name) if name else None
Defensive patterns
Strategy: validation
Validate before calling
if not name or name.isspace():
raise ValueError('cookie name required')
driver.get_cookie(name) Type guard
def is_valid_cookie_name(name) -> bool:
return isinstance(name, str) and bool(name) and not name.isspace() Prevention
- Normalize/strip cookie names from external sources before any cookie API.
- Centralize cookie name validation in a helper shared by get/delete.
When it happens
Trigger: Calling driver.get_cookie(''), driver.get_cookie(' '), or passing a name variable that resolved to an empty string (e.g. parsed from a header/config that was missing).
Common situations: Dynamically building the name from a config/CSV that has blanks; forgetting to validate user input before lookup; off-by-one slicing that yields ''.
Related errors
- Params must be an instance of CookieFilter. Received:'${cook
- missing 1 required keyword-only argument: 'options' (instanc
- browserName not specified in session capabilities
- No script with key: {script_key} existed in {self.pinned_scr
- Pinned script could not be found
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/e3bb0107d54d7dae.
Report an issue: GitHub.