0

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}
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67

2 Answers2

0

You can't simply encode or decode a dictionary, you need to iterate over each key and value and replace them by encoded ones. Also for dumping, you need to open the file in binary mode and there is a good example:

pickle.dump(driver.get_cookies(), open("cookie.fkle","wb"))

Easy way in here is to just encode each line (represents as a dictionary) within a function. Something like this:

import pickle

def encode_dict(input_dict):
    output = {}
    for key in input_dict:
        output.update({key.encode('utf-8'): input_dict[key].encode('utf-8')})
    return output

cookies = pickle.load(open("cookie.fkle", "rb"))
for cookie in cookies:
    print encode_dict(cookie)
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
0

M.R

Thanks alot

However I tweaked the answer a bit

output.update({str(key).encode('utf-8'): str(input_dict[key]).encode('utf-8')}) as I was getting a AttributeError: 'int' object has no attribute 'encode' python dictionary