How Python3 urllib use selenium cookie

1.2k views Asked by At

I use selenium simulation log in a website,and I want to use urllib with the cookie that it is get from selenium driver.But there is crash with this log.

AttributeError: 'SimpleCookie' object has no attribute 'domain'

My code is these please help me how to fix it.

cj = http.cookiejar.CookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(
    cookie_support)
all_cookies = driver.get_cookies()
cookies = {}
for s_cookie in all_cookies:
    cookies[s_cookie["name"]] = s_cookie["value"]
cookie = http.cookies.SimpleCookie()
cookie.load(cookies)
cj.set_cookie(cookie)
result = opener.open(url)
1

There are 1 answers

1
Saifur On

The error says it right.

You need to have correct Domain and expiry info to set a cookie with Selenium Why do not you simply grab the list of cookies and use that. There are some other factors involved when it comes to Selenium. See this

def test(self):
        driver = self.driver
        listcookies = driver.get_cookies()

        for s_cookie in listcookies:
            driver.add_cookie(s_cookie)