How to save and load cookies using Python + Selenium WebDriver

0
0

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.

  • You must to post comments
0
0

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

import pickle
import selenium.webdriver
driver
= selenium.webdriver.Firefox()
driver
.get("http://www.google.com")
pickle
.dump( driver.get_cookies() , open("cookies.pkl","wb"))

and later to add them back:

import pickle
import selenium.webdriver
driver
= selenium.webdriver.Firefox()
driver
.get("http://www.google.com")
cookies
= pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver
.add_cookie(cookie)
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website