我想将cookie会话从python-requests导出到selenium,我写这段代码:
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 = '''7|0|7|https://www.cartetitolari.mps.it/portaleTitolari/|FEAC78FFDF81D6121438D70986AF1C41|portale.titolari.client.service.PTService|login|portale.titolari.client.common.login.LoginRequest/3583069702|xxxxxxxxxxx|matteosbragia1984|1|2|3|4|1|5|5|0|0|6|7|'''
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()
for c in session.cookies :
driver.add_cookie({'name': c.name, 'value': c.value, 'path': c.path, 'expiry': c.expires})
print "dasdasds"
driver.get("https://www.cartetitolari.mps.it/portaleTitolari/titolari.html#H6VwDv5Ce0")
print "dasdasds2"
driver.get("https://www.cartetitolari.mps.it/portaleTitolari/downloadeco?id=0")
代码有效,但在页面加载时需要登录才能成功导出selenium中的会话/ cookie!哪里我错了?
答案 0 :(得分:0)
Cookie不会在浏览器首选项中浮动。它们始终与特定域或通配符域绑定,但它们始终具有域。
查看selenium-requests包,这似乎可以简化Cookie的创建。