Arete
Arete

Reputation: 1004

Passing cookies from Selenium to Python Requests

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...

Upvotes: 0

Views: 3695

Answers (1)

Max Daroshchanka
Max Daroshchanka

Reputation: 2968

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

Upvotes: 1

Related Questions