rezon
rezon

Reputation: 45

Remain logged into account using selenium

I'm trying to login to http://login.live.com, and stay logged in after closing the browser using pickle and cookies.

import pickle
from selenium import webdriver
browser = webdriver.Chrome() 
browser.get('https://login.live.com')
# i do my login here
pickle.dump(driver.get_cookies() , open("login_live.pkl","wb"))
browser.quit()

browser = webdriver.Chrome() 
browser.get('https://google.com')
for cookie in pickle.load(open("login_live.pkl", "rb")):
    driver.add_cookie(cookie)
browser.get('https://login.live.com')

The problem is that after directing to live.com, I don't remain logged into my account. I perform the same flow manually (obviously without loading cookies). Can't seem to figure out what is wrong, any help would be appreciated.

Upvotes: 0

Views: 2829

Answers (1)

Aritesh
Aritesh

Reputation: 2103

login.live.com is a redirection page and cookies are not associated with it. Use the page of cookies i.e. https://account.microsoft.com

So while re-loading the session, load the page and then load cookies -

import pickle
from selenium import webdriver
browser = webdriver.Chrome("./chromedriver") 
browser.get('https://login.live.com')
pickle.dump(browser.get_cookies() , open("login_live.pkl","wb"))
browser.quit()
browser = webdriver.Chrome("./chromedriver") 
browser.get('https://account.microsoft.com')
for cookie in pickle.load(open("login_live.pkl", "rb")):
    browser.add_cookie(cookie)

Upvotes: 1

Related Questions