loading cookies from selenium to mechanize with cookielib
Asked Answered
N

1

7

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'
Nasopharynx answered 30/11, 2013 at 21:55 Comment(0)
P
7

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

Pictorial answered 1/12, 2013 at 15:32 Comment(2)
thanks for your answer. I can now login to the site. A new problem has however appeared, when I collect links on the site this function "urlparse.urlparse(url).hostname" returns a None value, wich makes this function crash "mylinks.append("http://" + urlparse.urlparse(url).hostname + urlparse.urlparse(url).path)" due to the error message "TypeError: cannot concatenate 'str' and 'NoneType' objects". Do you know of any solutions to such a problem?Nasopharynx
@user3053161. happy to be of service. This new problem of yours is separate from your login cookie troubles, thus you should create a new separate question about itPictorial

© 2022 - 2024 — McMap. All rights reserved.