I'm trying to read cookies from a session and save it in a file(I'm saving it as python pickle file) and use it next time rather than logging into the application again. It saves a lot of time. I'm using Python 2.7.16rc1 and selenium web driver version 3.14.1.
I have tried saving the cookie which I get from driver.get_cookies()
. The issue is that the cookies are saved in the pickled object with Unicode encoding with `u in it which is causing the issue while reading it and sending it back.
I have tried to deal with the Unicode encoding option by reading the value in list one by one and applying UTF-8 encoding but getting a AttributeError: 'dict' object has no attribute 'encode'
The given below is the code to write the cookies to a pickle file
pickle.dump(driver.get_cookies(), open("cookie.fkle","w"))
The below codes reads it
import pickle
cookies = pickle.load(open("cookie.fkle", "rb"))
for cookie in cookies:
print cookie
I expect the output as below
{domain': google.com', name': lang', value': en', expiry': 1555427577, path': /', httpOnly': False, secure': False}
{domain': .google.com', name': _ga', value': GA1.2.1834438095.1554822783', expiry': 1617894782, path': /', httpOnly': False, secure': False}
{domain': .google.com', name': _gid', value': GA1.2.1839091575.1554822783', expiry': 1554909182, path': /', httpOnly': False, secure': False}
{domain': .google.com', name': _gat_gtag_UA_60214108_5', value': 1', expiry': 1554822842, path': /', httpOnly': False, secure': False}
But I'm getting the output as:
{u'domain': u'google.com', u'name': u'lang', u'value': u'en', u'expiry': 1555427577, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'.google.com', u'name': u'_ga', u'value': u'GA1.2.1834438095.1554822783', u'expiry': 1617894782, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'.google.com', u'name': u'_gid', u'value': u'GA1.2.1839091575.1554822783', u'expiry': 1554909182, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'.google.com', u'name': u'_gat_gtag_UA_60214108_5', u'value': u'1', u'expiry': 1554822842, u'path': u'/', u'httpOnly': False, u'secure': False}