cookie将会话导出到硒

cookie将会话导出到硒

本文介绍了python-requests cookie将会话导出到硒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将cookie会话从python-requests导出到selenium,我编写此代码:

I want to export cookies session from python-requests to selenium, im 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 = '''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")

当登录需要页面加载时,代码可以工作,但不能成功导出硒中的会话/ 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不在浏览器首选项中浮动。它们始终绑定到特定域或通配符域,但是它们始终具有域。

Cookies are not floating around in the browser preferences. They are always tied to a specific domain or a wildcard domain, but they always have a domain.

请查看包,它似乎简化了cookie的创建。

Have a look at selenium-requests package which seems to ease the creation of the cookies.

这篇关于python-requests cookie将会话导出到硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:00