30

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)

Goro
  • 9,919
  • 22
  • 74
  • 108

1 Answers1

29

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)
Vikas Ojha
  • 6,742
  • 6
  • 22
  • 35
  • Great, thanks. What is the purpose of updating headers in line #2, and where does the parameter 'headers' come from? – Goro Sep 18 '15 at 07:37
  • 2
    Just update my answer. Headers is just for sending any extra headers, most of the sites expect a user-agent string of popular browsers. – Vikas Ojha Sep 18 '15 at 07:40
  • 6
    Maybe it would be more pythonic to substitute the last three lines for: `s.cookies.update( {c['name']:c['value'] for c in driver.get_cookies()} )` – Nuno André Jul 03 '16 at 17:36
  • 3
    Will It work the other way around, can you start a selenium browser with the request session? – EndermanAPM Oct 05 '17 at 05:41
  • 3
    @EndermanAPM, Yes it will. But there is a limitation with Selenium Webdriver that you can add cookies only for current domain. So your code should have the following flow - Open Webpage using selenium, Add cookies using Webdriver, Open the Webpage again, and it will work. – Vikas Ojha Oct 05 '17 at 09:06
  • would it be possible to do other way around? Say I have active session with request.session(), can I pass it onto selenium? @VikasOjha – Burakhan Aksoy Apr 11 '21 at 20:09
  • 1
    @BurakhanAksoy, please check my above comment. Let me know if that doesnt help. – Vikas Ojha Apr 12 '21 at 06:39
  • @VikasOjha bummer :( I am trying to connect to linkedin. using selenium first and login triggers a captcha-like security step. When I solve captcha it logs in successfully. Any thoughts? But If I login with request.sessions() first, then I get error when I implement driver.add_cookies(cookies). It gives the following error -> selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'name' – Burakhan Aksoy Apr 12 '21 at 10:28