kingcope kingcope - 1 year ago 1076
Python Question

python-requests cookies export to selenium

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


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 = '''|FEAC78FFDF81D6121438D70986AF1C41|portale.titolari.client.service.PTService|login|portale.titolari.client.common.login.LoginRequest/3583069702|xxxxxxxxxxx|matteosbragia1984|''''', 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()


The code work, but not successfully export session/cookies in selenium, when the page loads are required to login! Where I'm wrong ?

Answer Source

You first need to navigate to the page to set the domain, then add each cookie by iterating the cookie jar:


for c in session.cookies :
    driver.add_cookie({'name':, 'value': c.value, 'path': c.path, 'expiry': c.expires})