Is it possible to "transfer" a session between selenium.webdriver and requests.session

Question:

In theory, if I copy all of the cookies from selenium’s webdriver object to requests.Session object, would requests be able to continue on as if the session was not interrupted?

Specifically, I am interested in writing automation where I get to specific location on the webpage via selenium, then pass on a certain download link to requests, which would download and verify specific bytes out of the file, and sometimes a full file. (The value of the file downloaded would change based on my interaction in selenium)

Asked By: Goro

||

Answers:

Yes it will definitely work. Following code snippet should help as well –

headers = {
"User-Agent":
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
}
s = requests.session()
s.headers.update(headers)

for cookie in driver.get_cookies():
    c = {cookie['name']: cookie['value']}
    s.cookies.update(c)
Answered By: Vikas Ojha