0

So, I am trying to get cookies from a browser with Selenium and use them for further requests in my console with the requests module. The example code is this:

from selenium import webdriver
import requests


header = {"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
s=requests.session()
driver=webdriver.Chrome()
driver.get("https://www.google.com/")  #using random website
x=driver.get_cookies()
print(x)
r=s.get("https://www.google.com/", headers=header, cookies=x)
print(r.status_code)

But when making the get request I always have following problem:

cookieselenium.py", line 19, in <module>
    r=s.get("https://www.bstn.com/", headers=header, cookies=x)
  File "C:\Users\patrick\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 546, in get
    return self.request('GET', url, **kwargs)
  File "C:\Users\patrick\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 519, in request
    prep = self.prepare_request(req)
  File "C:\Users\patrick\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 440, in prepare_request
    cookies = cookiejar_from_dict(cookies)
  File "C:\Users\patrick\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\cookies.py", line 524, in cookiejar_from_dict
    cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
TypeError: list indices must be integers or slices, not dict

I do not really know how to solve this, so I would be really thankful for any helpful answers...

crazynick
  • 1
  • 1

1 Answers1

0

you need to rebuild the cookie and use name and value only

cookies = {cookie['name']:cookie['value'] for cookie in x}
r=s.get("https://www.google.com/", headers=header, cookies=cookies)

# Or
for cookie in x:
    s.cookies.set(cookie['name'], cookie['value'])
r=s.get("https://www.google.com/", headers=header)
ewwink
  • 18,382
  • 2
  • 44
  • 54