1

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

x-Palace
  • 43
  • 7
  • 1
    You just need to check documentation of [selenium](https://selenium-python.readthedocs.io/navigating.html#cookies) and [requests](https://2.python-requests.org/en/v2.7.0/user/quickstart/#cookies). – Olvin Roght Oct 17 '20 at 10:50
  • @OlvinRoght I read. But still i didn't do it. My question is how can i transfer from selenium cookie to requests module. – x-Palace Oct 17 '20 at 11:35
  • Try to unindent 2 last lines. – Olvin Roght Oct 17 '20 at 11:42
  • @OlvinRoght i tried but still same.i can't use the session i get wih selenium callback({\r\n "errorMessage": "Unauthorized user",\r\n "errorDetails": "No cookie" – x-Palace Oct 17 '20 at 12:31

1 Answers1

0

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")