{"record":{"id":"e3bb0107d54d7dae","repo":"SeleniumHQ/selenium","slug":"cookie-name-cannot-be-empty","errorCode":null,"errorMessage":"Cookie name cannot be empty","messagePattern":"Cookie name cannot be empty","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"py/selenium/webdriver/remote/webdriver.py","lineNumber":754,"sourceCode":"            A list of dictionaries, corresponding to cookies visible in the\n            current session.\n        \"\"\"\n        return self.execute(Command.GET_ALL_COOKIES)[\"value\"]\n\n    def get_cookie(self, name) -> dict | None:\n        \"\"\"Get a single cookie by name (case-sensitive,).\n\n        Returns:\n             A cookie dictionary or None if not found.\n\n        Raises:\n            ValueError if the name is empty or whitespace.\n\n        Example:\n            `cookie = driver.get_cookie(\"my_cookie\")`\n        \"\"\"\n        if not name or name.isspace():\n            raise ValueError(\"Cookie name cannot be empty\")\n\n        with contextlib.suppress(NoSuchCookieException):\n            return self.execute(Command.GET_COOKIE, {\"name\": name})[\"value\"]\n\n        return None\n\n    def delete_cookie(self, name) -> None:\n        \"\"\"Delete a single cookie with the given name (case-sensitive).\n\n        Raises:\n            ValueError if the name is empty or whitespace.\n\n        Example:\n            `driver.delete_cookie(\"my_cookie\")`\n        \"\"\"\n        # Firefox deletes all cookies when \"\" is passed as name\n        if not name or name.isspace():\n            raise ValueError(\"Cookie name cannot be empty\")","sourceCodeStart":736,"sourceCodeEnd":772,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/remote/webdriver.py#L736-L772","documentation":"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.","triggerScenarios":"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).","commonSituations":"Dynamically building the name from a config/CSV that has blanks; forgetting to validate user input before lookup; off-by-one slicing that yields ''.","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."],"exampleFix":"# before\nc = driver.get_cookie(name)  # name may be ''\n\n# after\nname = (name or '').strip()\ncookie = driver.get_cookie(name) if name else None","handlingStrategy":"validation","validationCode":"if not name or name.isspace():\n    raise ValueError('cookie name required')\ndriver.get_cookie(name)","typeGuard":"def is_valid_cookie_name(name) -> bool:\n    return isinstance(name, str) and bool(name) and not name.isspace()","tryCatchPattern":null,"preventionTips":["Normalize/strip cookie names from external sources before any cookie API.","Centralize cookie name validation in a helper shared by get/delete."],"tags":["cookies","validation","webdriver"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}