VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-save-and-load-cookies-in-selenium-using-python/

⇱ How to save and load cookies in Selenium using Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to save and load cookies in Selenium using Python

Last Updated : 23 Jul, 2025

In web automation and testing, maintaining session information across different runs of scripts is often necessary. Cookies are a great way to save session data to avoid logging in repeatedly. Selenium, a popular tool for web testing, provides straightforward ways to save and load cookies using Python.

In this article, we will learn all the steps to handle cookies efficiently in Selenium.

Working with Cookies in Selenium

A cookie is a small piece of data that is sent from a website/server and stored on our computer. Cookies are mostly used to recognize the user and load the stored information. We can add, delete, and read cookies, which makes it convenient to manage sessions.

Key operations with cookies in Selenium:

  • Adding a Cookie: Add a cookie to the current session using the add_cookie() method.
  • Getting All Cookies: Retrieve all cookies stored in the current session with the get_cookies() method.
  • Deleting Cookies: Remove cookies from the session using the delete_cookie() method.

1. Saving Cookies to `cookies.pkl`

In order to maintain the session details, we can write cookies into a file. This comes in handy when it becomes necessary to carry on with a session without signing back in.

The following code snippet saves cookies to a file after logging into a website:

Output: We get a cookies.pkl file in the current working directory. This file contains the saved cookies in binary format.

πŸ‘ Screenshot-2024-09-06-111409
cookies.pkl


πŸ‘ Screenshot-2024-09-06-111250
Saving cookies using Python Selenium

2. Loading Cookies from cookies.pkl

To resume a session, we can load cookies from a file and add them back to the browser using Selenium.

The following code snippet loads cookies from a file and uses them to restore the session:

  • Load Cookies: Read the cookies.pkl file and load the cookies into the browser using driver.add_cookie().
  • Refresh the Page: Refresh the browser to apply the loaded cookies.

3. Saving Cookies to cookies.json

Instead of using pickle, we can use Python's json module to save the cookies in a human-readable cookies.json file.

Output:

πŸ‘ Screenshot-2024-09-06-113521
Saving cookies in json in selenium python

4. Loading Cookies from cookies.json

We can usejson.load() to read the cookies from the cookies.json file and load them into the browser as before.

To load cookies from a cookies.json file, use the following code:

Best Practices

  • Secure Storage: In particular, when it comes to sensitive data stored in cookies make sure to secure them properly. Do not allow this data to be accessed from source control or logs.
  • Cookie Expiration: Refer to the expiration date prior to loading cookies to manage cookie expiration. If they aren’t working anymore, re-authenticate and save new ones.
  • SameSite attribute: Ensure that when utilizing cookies across varying domains to prevent probable difficulties with sharing of cookies.

Conclusion

The strategy comprising Storage as well as Load cookies in Selenium using Python is simple but it is a very powerful technique for managing sessions during web tests. If cookies are kept persistently one will not have to log in again and again because all the automation will be easy. This method suits particularly those test scripts which have to preserve a session state after many executions.

Comment
Article Tags: