How can I load session and cookies from Selenium browser? The following code:

import requests

cookies = [{u'domain': u'',
            u'name': u'ASPSESSIONIDAEQDTQRB',
            u'value': u'ADGIJGJDDGLFIIOCEZJHJCGC',
            u'expiry': None, u'path': u"",
            u'secure': True}]
response = requests.get(url2, cookies=cookies)

gives me the following exception:

Traceback (most recent call last):
  File "F:\PYTHON\python_scripts\cookies\", line 23, in <module>
    response = requests.get(url2, cookies=cookies)
  File "C:\Python27\lib\site-packages\requests\", line 68, in get
    return request('get', url, **kwargs)<br/>
  File "C:\Python27\lib\site-packages\requests\", line 450, in request
    prep = self.prepare_request(req)
    cookies = cookiejar_from_dict(cookies)
  File "C:\Python27\lib\site-packages\requests\", line 439, in cookiejar_from_dict
    cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
TypeError: list indices must be integers, not dict

First you have to get the cookies from your driver instance:

cookies = driver.get_cookies()

This returns cookie dictionaries for your session.

Next, set those cookies in requests:

s = requests.Session()
for cookie in cookies:
    s.cookies.set(cookie['name'], cookie['value'])

You can use 3rd party package like selenium-requests or requestium. They provide function to share cookies between requests and selenium.

I made this solution, it’s solved unexpected keyword arguments expiry

def set_cookies(cookies, s):
    for cookie in cookies:
        if 'httpOnly' in cookie:
            httpO = cookie.pop('httpOnly')
            cookie['rest'] = {'httpOnly': httpO}
        if 'expiry' in cookie:
            cookie['expires'] = cookie.pop('expiry')
    return s

I think this doesn’t directly answer the question but if your goal is to make requests pretending to be the same session that selenium is, you can do this:

resp = browser.execute_script(f'''
    var xmlHttp = new XMLHttpRequest(); "GET", "{yourUrl}", false );
    xmlHttp.send( null );
    return xmlHttp.responseText;