是否有可能"传输" selenium.webdriver和requests.session之间的会话 [英] Is it possible to "transfer" a session between selenium.webdriver and requests.session

查看:471
本文介绍了是否有可能"传输" selenium.webdriver和requests.session之间的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在理论上,如果我复制所有的cookie硒的的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?

具体来说,我感兴趣的是写自动化在那里我得到特定位置上通过硒的网页,然后在一定的下载链接传递给要求,它会下载并验证出文件的特定字节,有时一个完整的文件。 (下载的文件的价值将改变根据我的硒交互)

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)

推荐答案

是的,它肯定会工作。继code段应有所帮助 -

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天全站免登陆