Skip to content Skip to sidebar Skip to footer

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 session=requests.Sessio

Solution 1:

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})

Solution 2:

I had a similar issue. Watching with the developer window, I could see that after login a cookie was being sent but then the page via javascript or something else was redirecting before returning control to the program. So, I was unable to get that cookie and save it off.

After more research I realised that the program was starting with a clean session each time (this answer helped a lot) so the persistent cookies weren't persistent at all. It took further research, but giving selenium (via splinter) a profile to work with resolved my issue.

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("user-data-dir=" + tdir + "/chrome-session")
chrome_options.add_argument("--profile-directory=Default")
with Browser('chrome', headless=True, options=chrome_options) as browser:

Post a Comment for "Python-requests Cookies Export To Selenium"