Back
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 can save the current cookies as a python object using pickle. For example:
import pickleimport selenium.webdriverdriver = selenium.webdriver.Firefox()driver.get("http://www.google.com") pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
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 pickleimport 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)
import selenium.webdriver driver = selenium.webdriver.Firefox()
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
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!
30.9k questions
32.9k answers
500 comments
665 users