Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

How can I save all cookies in Python's Selenium WebDriver to a txt-file, then load them later? The documentation doesn't say much of anything about the getCookies function.

1 Answer

0 votes
by (62.9k points)

You can save the current cookies as a python object using pickle. For example:

import pickle

import selenium.webdriver

driver = selenium.webdriver.Firefox()


pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

and later to add them back:

import pickle

import selenium.webdriver driver = selenium.webdriver.Firefox() 


cookies = pickle.load(open("cookies.pkl", "rb"))

for cookie in cookies: 


If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training

Browse Categories