Efficient Login Session Management in Selenium-Python Save and Reuse Credentials for Browser Automation

Ghulam Mustafa
3 min readMay 29, 2023

--

Introduction

Selenium with Python is a powerful combination for web automation, allowing developers to automate tasks and perform testing on websites. One common scenario is to automate login processes, but repeatedly logging in can be time-consuming and inefficient. In this blog post, we will explore efficient login session management techniques using Selenium-Python, enabling us to save and reuse credentials for seamless Browser automation.

Methods

There are various methods to maintain a login session across multiple instances of Selenium or across different runs of your script, you can use the following approaches.

Here I will share the easiest way to solve this issue.

1. Use cookies

Use cookies: After successfully logging in, you can extract the session cookies using Selenium and store them. Then, for subsequent requests, you can add the stored cookies to the WebDriver to maintain the session.

Step 1.

Log into your website manually or by selenium. After successfully logging in, you can extract the session cookies using Selenium by calling the driver.get_cookies() method. This will retrieve all the cookies associated with the current session.

# Get and store cookies after login
cookies = driver.get_cookies()

Step .2

Once you have the cookies, you can store them in a variable or a file for later use. For example, you can store them in a JSON file:

import json

# Store cookies in a file
with open('cookies.json', 'w') as file:
json.dump(cookies, file)

Step 3.

Now next time (after close the browser) for subsequent requests, you can add the stored cookies to the WebDriver using the driver.add_cookie() method. This will set the cookies for the current session and allow you to maintain the login session.

# Load cookies to a vaiable from a file
with open('cookies.json', 'r') as file:
cookies = json.load(file)

# Goto the same URL
driver.get('URL')

# Set stored cookies to maintain the session
for cookie in cookies:
driver.add_cookie(cookie)

Once the cookies are added to the WebDriver, you can continue interacting with the website, and the session will be maintained. Selenium will automatically include the cookies in the HTTP requests sent by the WebDriver, allowing you to access authenticated pages or perform actions as if you were logged in.

Here is the GitHub link with example code using this method.

Note:

It’s important to note that cookies have an expiration time, and their validity may vary depending on the website’s session management. If a cookie expires or becomes invalid, you may need to repeat the login process and update the stored cookies accordingly.

By using cookies, you can persist the login session across multiple instances of the WebDriver or even across different script executions. This method is especially useful when you want to maintain a session for a prolonged period or perform actions that require authentication in subsequent runs of your script.

Remember to handle the storage and retrieval of cookies securely, as they contain sensitive session information.

That’s it. I hope this post helped you solve this issue. And if you still facing any problem while using this code don’t hesitate to ask me in the comment section or contact me.

--

--

Ghulam Mustafa

Senior Software Developer and Python Automation Expert.