{"record":{"id":"f0f515fdbd5c74f6","repo":"SeleniumHQ/selenium","slug":"storage-state-file-not-found-file-path","errorCode":null,"errorMessage":"Storage state file not found: {file_path}","messagePattern":"Storage state file not found: (.+?)","errorType":"validation","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/common/api_request_context.py","lineNumber":566,"sourceCode":"        fail_on_status_code: bool = False,\n    ) -> \"_IsolatedAPIRequestContext\":\n        \"\"\"Create an isolated API request context that does not sync with the browser.\n\n        Args:\n            base_url: Optional base URL for this context.\n            extra_headers: Optional headers for this context.\n            storage_state: Optional cookies to pre-load, as a dict, JSON file path, or Path.\n            fail_on_status_code: If True, raise APIRequestFailure for non-2xx responses.\n\n        Returns:\n            An _IsolatedAPIRequestContext instance.\n        \"\"\"\n        cookies: list[dict] = []\n        if storage_state is not None:\n            if isinstance(storage_state, (str, pathlib.Path)):\n                file_path = pathlib.Path(storage_state)\n                if not file_path.exists():\n                    raise FileNotFoundError(f\"Storage state file not found: {file_path}\")\n                try:\n                    with open(file_path) as f:\n                        state = json.load(f)\n                except json.JSONDecodeError as e:\n                    raise ValueError(f\"Invalid JSON in storage state file {file_path}: {e}\") from e\n                except OSError as e:\n                    raise OSError(f\"Cannot read storage state file {file_path}: {e}\") from e\n            else:\n                state = storage_state\n            cookies = list(state.get(\"cookies\", []))\n\n        return _IsolatedAPIRequestContext(\n            base_url=base_url,\n            extra_headers=extra_headers,\n            cookies=cookies,\n            timeout=self._timeout,\n            max_redirects=self._max_redirects,\n            fail_on_status_code=fail_on_status_code,","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/common/api_request_context.py#L548-L584","documentation":"When `new_context(storage_state=...)` is given a string or pathlib.Path, Selenium treats it as a file path to a JSON storage-state file. If that path does not exist, it raises `FileNotFoundError`. The storage state file holds cookies (and optionally origins) to pre-seed the request context.","triggerScenarios":"`new_context(storage_state=\"/nope/state.json\")`, `new_context(storage_state=Path(\"state.json\"))` where the file was never created, or a relative path that does not resolve from the CWD.","commonSituations":"CI runs where the storage-state artifact was not generated/downloaded. Path typos. Relative paths breaking across machines. Generating the file in a previous step that failed.","solutions":["Verify the path exists: `if not pathlib.Path(p).exists(): raise ...` before calling.","Generate the file first via `storage_state(path=...)` from a logged-in session.","Pass a dict directly to skip the file: `new_context(storage_state={\"cookies\": [...]})`.","Use an absolute path anchored to the script location."],"exampleFix":"// before\nctx = api.new_context(storage_state=\"state.json\")  # FileNotFoundError\n// after\nimport pathlib\np = pathlib.Path(__file__).parent / \"state.json\"\nctx = api.new_context(storage_state=str(p))  # after generating it","handlingStrategy":"validation","validationCode":"import pathlib\np = pathlib.Path(\"state.json\")\nassert p.exists(), f\"storage state file missing: {p}\"\nctx = api.new_context(storage_state=str(p))","typeGuard":"import pathlib\ndef storage_state_file_exists(value) -> bool:\n    return pathlib.Path(value).exists()","tryCatchPattern":"try:\n    ctx = api.new_context(storage_state=path)\nexcept FileNotFoundError:\n    ctx = api.new_context(storage_state={\"cookies\": []})  # fallback to empty state","preventionTips":["Generate the file with storage_state(path=...) before reusing it.","Use absolute paths anchored to the script directory.","Pass a dict directly to avoid filesystem dependencies."],"tags":["storage-state","filesystem","api-request","cookies"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}