Get cookies from selenium to requests

Viewed 430

I can login to a website with selenium and i can receive all cookies. But then I have to quickly submit a request to the site. Meanwhile, selenium stays very slow. That's why I want to receive cookies with selenium and send requests via the request module.

My Selenium Code (First I log in to the website and received all cookies with the code below.)

browser.get('https://www.example.com/login')
cookiem1 = browser.get_cookies()
print(cookiem1)

2nd stage, I will go to another page of the website and make a request.

s = requests.Session()
for cookie in cookiem1:
   s.cookies.set(cookie['name'], cookie['value'])
   r = s.get("https://example.com/postcomment')
   print(r.content)

I use cookies in this way, but when I send the url via request module, the site does not autohorize my user.

My error: "errorMessage": "Unauthorized user",\r\n "errorDetails": "No cookie"

Probably with this code the site doesn't unauthorized my session

Thanks in advance

1 Answers

try this

    import requests as re
    ck = browser.get_cookies()
    s = re.Session()
    c = [s.cookies.set(c['name'], c['value']) for c in ck]
    response = s.get("https://example.com/postcomment")
Related