是否可以“转让"?selenium.webdriver 和 requests.session 之间的会话 [英] Is it possible to "transfer" a session between selenium.webdriver and requests.session

查看:32
本文介绍了是否可以“转让"?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)

这篇关于是否可以“转让"?selenium.webdriver 和 requests.session 之间的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆