python-requests cookie导出到硒 [英] python-requests cookies export to selenium
本文介绍了python-requests cookie导出到硒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想登录网站丝毫请求库,然后将cookie导出到硒中,然后编写此代码:
I want to login to website whit requests library and after export cookies to selenium, I'm write this code :
import requests
from selenium import webdriver
session=requests.Session()
MyHeaderss = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.32 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.32", "X-GWT-Permutation" : "6FEFBE57C6E73F0AB33BD5A4E17945DE", "Content-Type":"text/x-gwt-rpc; charset=utf-8"}
login_data = '''https://www.cartetitolari.mps.it/portaleTitolari/|FEAC78FFDF81D6121438D70986AF1C41|portale.titolari.client.service.PTService|login|portale.titolari.client.common.login.LoginRequest/3583069702|xxxxxxxxxxx|matteosbragia1984|'''
ra0=session.post('https://www.cartetitolari.mps.it/portaleTitolari/service', data=login_data, headers=MyHeaderss)
print ra0.content
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.32 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.32")
driver = webdriver.Firefox()
driver.add_cookie(session.cookies.get_dict())
driver.get("https://www.cartetitolari.mps.it/portaleTitolari/downloadeco?id=0")
当登录需要页面加载时,代码可以工作,但不能成功导出硒中的会话/cookie!我哪里错了?
The code work, but not successfully export session/cookies in selenium, when the page loads are required to login! Where I'm wrong ?
推荐答案
您首先需要导航到页面以设置域,然后通过迭代cookie罐添加每个cookie:
You first need to navigate to the page to set the domain, then add each cookie by iterating the cookie jar:
driver.get("https://www.cartetitolari.mps.it/portaleTitolari/titolari.html")
for c in session.cookies :
driver.add_cookie({'name': c.name, 'value': c.value, 'path': c.path, 'expiry': c.expires})
这篇关于python-requests cookie导出到硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文