Python - Requests, Selenium - passing cookies while logging in Python - Requests, Selenium - passing cookies while logging in selenium selenium

Python - Requests, Selenium - passing cookies while logging in


I finally found out what the problem was.Before making the post request with the requests library, I should have passed the cookies of the browser first. The code is as follows:

import requestsfrom selenium import webdriverdriver = webdriver.Firefox()url = "some_url" #a redirect to a login page occursdriver.get(url)#storing the cookies generated by the browserrequest_cookies_browser = driver.get_cookies()#making a persistent connection using the requests libraryparams = {'os_username':'username', 'os_password':'password'}s = requests.Session()#passing the cookies generated from the browser to the sessionc = [s.cookies.set(c['name'], c['value']) for c in request_cookies_browser]resp = s.post(url, params) #I get a 200 status_code#passing the cookie of the response to the browserdict_resp_cookies = resp.cookies.get_dict()response_cookies_browser = [{'name':name, 'value':value} for name, value in dict_resp_cookies.items()]c = [driver.add_cookie(c) for c in response_cookies_browser]#the browser now contains the cookies generated from the authentication    driver.get(url)


I had some issues with this code because its set double cookies to the original browser cookie (before login) then I solve this with cleaning the cookies before set the login cookie to original. I used this command:

driver.delete_all_cookies()