是否可以“转让"?selenium.webdriver 和 requests.session 之间的会话

是否可以“转让

问题描述:

理论上,如果我将所有 cookie 从 selenium 的 webdriver 对象复制到 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?

具体来说,我对编写自动化感兴趣,我通过 selenium 到达网页上的特定位置,然后将某个下载链接传递给 requests,这将从中下载并验证特定字节文件,有时是一个完整的文件.(下载文件的值会根据我在 selenium 中的交互而改变)

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)

是的,它肯定会奏效.以下代码片段也应该有所帮助 -

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)