Python: use cookie to login with Selenium
You can use pickle
to save cookies as text file and load it later:
def save_cookie(driver, path):
with open(path, 'wb') as filehandler:
pickle.dump(driver.get_cookies(), filehandler)
def load_cookie(driver, path):
with open(path, 'rb') as cookiesfile:
cookies = pickle.load(cookiesfile)
for cookie in cookies:
driver.add_cookie(cookie)
I would advise in using json format, because the cookies are inherently dictionaries and lists. Otherwise this is the approved answer.
import json
def save_cookie(driver, path):
with open(path, 'w') as filehandler:
json.dump(driver.get_cookies(), filehandler)
def load_cookie(driver, path):
with open(path, 'r') as cookiesfile:
cookies = json.load(cookiesfile)
for cookie in cookies:
driver.add_cookie(cookie)