Save and Load Cookies in Selenium with Python
Selenium's WebDriver exposes the current browser's cookies through get_cookies, and accepts them back through add_cookie.
Combined with Python's pickle module, that pair lets you write a session's cookies to disk and re-inject them into a fresh driver instance. This guide walks through the save/load loop, the ordering rules that make it work, and the cookie fields worth checking before you trust a restored session.
How the save and load loop works
Two WebDriver methods do the work. get_cookies returns a list of dictionaries, one per cookie, each carrying fields such as name, value, domain, path, expiry, secure and httpOnly. add_cookie accepts a single dictionary in the same shape and writes it into the current browser session.
Python's pickle module serializes that list to a binary file and reads it back later. The pattern is: open the target page, call get_cookies, dump the list to a .pkl file, then in a later run load the list and loop over it calling add_cookie for each entry. The exact reference code accompanies this page as a protected module and should be treated as the authoritative snippet rather than retyped from prose.
Ordering rules that make restoration work
Cookies are scoped to a domain, so the driver must already be on the matching domain before add_cookie is called. If you add cookies while the browser is on about:blank or a different host, the browser will reject them or attach them to the wrong context. Navigate first, then restore.
A second ordering point: save cookies after the page has finished setting them. If you call get_cookies immediately after driver.get, a page that sets cookies via JavaScript may not have finished, and the saved file will be incomplete. A short wait or an explicit wait for a known element is usually enough.
Cookie field checklist before you trust a restored session
Each cookie dictionary returned by get_cookies carries fields that affect whether the browser will accept it back. Check these before relying on a restored session:
- name and value: the actual cookie payload.
- domain: must match the host the driver is currently on, or a parent domain the browser permits.
- path: usually /, but a narrower path limits where the cookie is sent.
- expiry: a Unix timestamp; an expired cookie will be discarded on restore.
- secure: if true, the cookie is only sent over HTTPS, so restoring it on an HTTP page will not behave as expected.
- httpOnly: if true, the cookie is not readable from JavaScript but is still settable via add_cookie.
If any of these look wrong after a restore, the cause is usually a domain or path mismatch rather than a problem with pickle itself.
Pickle or JSON for the cookie file?
Pickle is the shortest path when the save and load both happen in Python, because it round-trips the list of dictionaries without extra conversion. The trade-off is portability: a .pkl file is Python-specific and should not be loaded from an untrusted source, since unpickling can execute code.
If the cookie file needs to be read by another language, inspected by hand, or stored somewhere you do not fully control, dumping the same list as JSON with json.dump and reading it back with json.load is the safer choice. The WebDriver calls (get_cookies and add_cookie) are identical either way; only the serialization step changes.
Frequently asked questions
Why do my restored cookies get rejected by Selenium?
The most common cause is that the driver is not on the cookie's domain when add_cookie is called. Navigate to the target URL first, then loop through the saved cookies. A path or secure-flag mismatch can also cause a cookie to be silently dropped.
Is it safe to load a pickle file of cookies?
Only if you created the file yourself or fully trust its source. Unpickling can execute arbitrary code, so a .pkl file from an untrusted party is a security risk. For shared or inspected cookie files, use JSON instead.
Can Selenium restore httpOnly cookies?
Yes. httpOnly prevents JavaScript from reading the cookie, but add_cookie operates at the WebDriver level and can set it. The cookie will still be sent with matching requests.