I would like to integrate python Selenium and Requests modules to authenticate on a website.
I am using the following code:
JavaScript
x
15
15
1
import requests
2
from selenium import webdriver
3
4
driver = webdriver.Firefox()
5
url = "some_url" #a redirect to a login page occurs
6
driver.get(url) #the login page is displayed
7
8
#making a persistent connection to authenticate
9
params = {'os_username':'username', 'os_password':'password'}
10
s = requests.Session()
11
resp = s.post(url, params) #I get a 200 status_code
12
13
#passing the cookies to the driver
14
driver.add_cookie(s.cookies.get_dict())
15
The problem is that when I enter the browser the login authentication is still there when I try to access the url
even though I passed the cookies generated from the requests session.
How can I modify the code above to get through the authentication web page?
Advertisement
Answer
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:
JavaScript
1
27
27
1
import requests
2
from selenium import webdriver
3
4
driver = webdriver.Firefox()
5
url = "some_url" #a redirect to a login page occurs
6
driver.get(url)
7
8
#storing the cookies generated by the browser
9
request_cookies_browser = driver.get_cookies()
10
11
#making a persistent connection using the requests library
12
params = {'os_username':'username', 'os_password':'password'}
13
s = requests.Session()
14
15
#passing the cookies generated from the browser to the session
16
c = [s.cookies.set(c['name'], c['value']) for c in request_cookies_browser]
17
18
resp = s.post(url, params) #I get a 200 status_code
19
20
#passing the cookie of the response to the browser
21
dict_resp_cookies = resp.cookies.get_dict()
22
response_cookies_browser = [{'name':name, 'value':value} for name, value in dict_resp_cookies.items()]
23
c = [driver.add_cookie(c) for c in response_cookies_browser]
24
25
#the browser now contains the cookies generated from the authentication
26
driver.get(url)
27