pythoncookiesseleniummechanizecookielib

loading cookies from selenium to mechanize with cookielib


I am trying to login to a website with selenium, then transfer the cookie to mechanize. I have successfully logged in with selenium and saved its session cookie to a variable. The problem comes when trying to load the cookie with cookielib.

Relevant coding:

.
. #loging in to website with selenium
.
cookie = browser.get_cookies()   #save the session cookie from selenium to variable "cookie"
.
. #starting up mechanize
.
cj = cookielib.LWPCookieJar() 
.
.
.
cj.set_cookie(cookie) #load cookie from selenium

the problem appear when setting the cookie with cj.set_cookie function, and I get the following error message

File "..../cookielib.py", line 1627, in set_cookie
if cookie.domain not in c: c[cookie.domain] = {}
AttributeError: 'list' object has no attribute 'domain'

Solution

  • if you print the cookies collected by Selenium and compare it to a cookie collected by mechanize/cookielib you will notice they use different formats.

    To overcome this you can try something like this (you may need to modify it a bit, to fit your needs. but you get the general idea):

    cj = cookielib.LWPCookieJar()
    
    for s_cookie in cookie:
        cj.set_cookie(cookielib.Cookie(version = 0, name = s_cookie['name'], value = s_cookie['value'], port = '80', port_specified = False, domain = s_cookie['domain'], domain_specified = True, domain_initial_dot = False, path = s_cookie['path'], path_specified = True, secure = s_cookie['secure'], expires = s_cookie['expiry'], discard = False, comment = None, comment_url = None, rest = None, rfc2109 = False))
    

    A bit more fancy solution would be to iterate over the selenium cookies and make a dictionary with name : value pairs