Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 292
Goro Avatar asked Sep 17 '15 20:09

Goro


People also ask

Can you use Selenium and requests together?

Extends Selenium WebDriver classes to include the request function from the Requests library, while doing all the needed cookie and request headers handling.

Which is better Selenium or requests?

Using Requests generally results in faster and more concise code, while using Selenium makes development faster on Javascript heavy sites.

How does Selenium handle multiple sessions?

New Selenium IDE To create multiple sessions, we shall add the attributes – parallel and thread-count in the XML file. The attribute thread-count controls the number of sessions to be created while executing the tests in a parallel mode. The value of parallel attribute is set to methods.

What is session in requests?

Session object allows one to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance and will use urllib3's connection pooling.


1 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) 
like image 148
Vikas Ojha Avatar answered Sep 22 '22 10:09

Vikas Ojha