0

I am making a Python 3-script with Selenium 4 and Gecko web driver. I am using cookies = driver.get_cookies() to capture cookies after logging in to a site.

The question is how I can cookies from Selenium in a GET request using the Requests module. In other words, how can we capture cookies with Selenium and use those cookies in Requests?

I tried the suggested answer in this question, but it is not correct and the question is over a year old without any other answers...

Arete
  • 948
  • 3
  • 21
  • 48
  • Does [this](https://stackoverflow.com/questions/59532784/error-when-loading-cookies-into-a-python-request-session/59843387#59843387) or [this](https://stackoverflow.com/questions/59951304/how-to-save-login-data-to-be-recognized-for-python-selenium-webdriver/59957467#59957467) discussion helps you? – undetected Selenium Jan 05 '22 at 15:12

1 Answers1

1

Try this way:

cookies = driver.get_cookies()
requests_cookies = {}
for c in cookies:
    requests_cookies[c['name']] = c['value']    

response = requests.get('http://some-host...', cookies=requests_cookies)    

Reference:

https://medium.com/geekculture/how-to-share-cookies-between-selenium-and-requests-in-python-d36c3c8768b

Max Daroshchanka
  • 2,698
  • 2
  • 10
  • 14
  • 1
    This worked! Although I completely forgot to add a credible `User-Agent` to the request header and was quickly rejected from the site Once I added it worked. Thanks! – Arete Jan 05 '22 at 19:50