17

What I want to do is to open a page (for example youtube) and be automatically logged in, like when I manually open it in the browser.

From what I've understood, I have to use cookies, the problem is that I can't understand how.

I tried to download youtube cookies with this:

driver = webdriver.Firefox(executable_path="driver/geckodriver.exe")
driver.get("https://www.youtube.com/")
print(driver.get_cookies())

And what I get is:

{'name': 'VISITOR_INFO1_LIVE', 'value': 'EDkAwwhbDKQ', 'path': '/', 'domain': '.youtube.com', 'expiry': None, 'secure': False, 'httpOnly': True}

So what cookie do I have to load to automatically log in?

3

6 Answers 6

26

You can use pickle to save cookies as text file and load it later:

def save_cookie(driver, path):
    with open(path, 'wb') as filehandler:
        pickle.dump(driver.get_cookies(), filehandler)

def load_cookie(driver, path):
     with open(path, 'rb') as cookiesfile:
         cookies = pickle.load(cookiesfile)
         for cookie in cookies:
             driver.add_cookie(cookie)
8
  • Ok, so I manually did the login and I saved the cookie, now in what part of the code do I have to load them? I tried to load them after driver.get() but nothing happens.
    – L'ultimo
    Commented Jul 31, 2017 at 16:57
  • In most of time you have to: 1. go home page (it needs to be a page within the same domain), 2. load cookies, 3. refresh page
    – Arount
    Commented Jul 31, 2017 at 19:11
  • @Arount, what do mean by "1. go home page"? Thanks. Commented Jan 5, 2018 at 16:56
  • @RatmirAsanov You have to load a page of the same domain than the page you want to access. Usually I use home page, the "index".
    – Arount
    Commented Jan 7, 2018 at 20:29
  • 2
    @DannyWatson maybe you should try to understand and not to copy pasta. That could leads you to the solution. Code is not maid by browsing stackoverflow. o/
    – Arount
    Commented Dec 21, 2018 at 13:09
12

I would advise in using json format, because the cookies are inherently dictionaries and lists. Otherwise this is the approved answer.

import json

def save_cookie(driver, path):
    with open(path, 'w') as filehandler:
        json.dump(driver.get_cookies(), filehandler)

def load_cookie(driver, path):
    with open(path, 'r') as cookiesfile:
        cookies = json.load(cookiesfile)
    for cookie in cookies:
        driver.add_cookie(cookie)
2
  • 3
    Also, as estated in the Python documentation (docs.python.org/3/library/pickle.html), the pickle module is not secure because it is possible to construct malicious pickle data which will execute arbitrary code during unpickling.
    – rpet
    Commented Feb 15, 2022 at 19:32
  • 1
    I also prefer JSON over pickle for serialization. Pickle can be risky because unpickling data can execute arbitrary code, making it a security concern. Using pickle to transfer data between programs or store data between sessions can introduce vulnerabilities. JSON, on the other hand, is a safer choice as it doesn't introduce these security risks. It is a standardized data interchange format that is widely supported across various programming languages. This makes it a more reliable option for sharing data between different programs and ensuring compatibility.
    – Elon Fask
    Commented Oct 10, 2023 at 14:20
7

I had a scenario where I would like to reuse once authenticated/logged-in sessions. I'm using multiple browser simultaneously.

I've tried plenty of solutions from blogs and StackOverflow answers.

1. Using user-data-dir and profile-directory

These chrome options which solves purpose if you opening one browser at a time, but if you open multiple windows it'll throw an error saying user data directory is already in use.

2. Using cookies

Cookies can be shared across multiple browsers. Code available in SO answers are have most of the important blocks on how to use cookies in selenium. Here I'm extending those solutions to complete the flow.

Code

# selenium-driver.py
import pickle
from selenium import webdriver


class SeleniumDriver(object):
    def __init__(
        self,
        # chromedriver path
        driver_path='/Users/username/work/chrome/chromedriver',
        # pickle file path to store cookies
        cookies_file_path='/Users/username/work/chrome/cookies.pkl',
        # list of websites to reuse cookies with
        cookies_websites=["https://facebook.com"]

    ):
        self.driver_path = driver_path
        self.cookies_file_path = cookies_file_path
        self.cookies_websites = cookies_websites
        chrome_options = webdriver.ChromeOptions()
        self.driver = webdriver.Chrome(
            executable_path=self.driver_path,
            options=chrome_options
        )
        try:
            # load cookies for given websites
            cookies = pickle.load(open(self.cookies_file_path, "rb"))
            for website in self.cookies_websites:
                self.driver.get(website)
                for cookie in cookies:
                    self.driver.add_cookie(cookie)
                self.driver.refresh()
        except Exception as e:
            # it'll fail for the first time, when cookie file is not present
            print(str(e))
            print("Error loading cookies")

    def save_cookies(self):
        # save cookies
        cookies = self.driver.get_cookies()
        pickle.dump(cookies, open(self.cookies_file_path, "wb"))

    def close_all(self):
        # close all open tabs
        if len(self.driver.window_handles) < 1:
            return
        for window_handle in self.driver.window_handles[:]:
            self.driver.switch_to.window(window_handle)
            self.driver.close()

    def quit(self):
        self.save_cookies()
        self.close_all()


def is_fb_logged_in():
    driver.get("https://facebook.com")
    if 'Facebook – log in or sign up' in driver.title:
        return False
    else:
        return True


def fb_login(username, password):
    username_box = driver.find_element_by_id('email')
    username_box.send_keys(username)

    password_box = driver.find_element_by_id('pass')
    password_box.send_keys(password)

    login_box = driver.find_element_by_id('loginbutton')
    login_box.click()


if __name__ == '__main__':
    """
    Run  - 1
    First time authentication and save cookies

    Run  - 2
    Reuse cookies and use logged-in session
    """
    selenium_object = SeleniumDriver()
    driver = selenium_object.driver
    username = "fb-username"
    password = "fb-password"

    if is_fb_logged_in(driver):
        print("Already logged in")
    else:
        print("Not logged in. Login")
        fb_login(username, password)

    selenium_object.quit()

Run 1: Login & Save Cookies

$ python selenium-driver.py
[Errno 2] No such file or directory: '/Users/username/work/chrome/cookies.pkl'
Error loading cookies
Not logged in. Login

This will open facebook login window and enter username-password to login. Once logged-in it'll close the browser and save cookies.

Run 2: Reuse cookies to continue loggedin session

$ python selenium-driver.py
Already logged in

This will open logged in session of facebook using stored cookies.

Requirements

  • Python 3.7
  • Selenium Webdriver
  • Pickle
2

Here is one possible solution

import pickle
from selenium import webdriver

def save_cookie(driver):
    with open("cookie", 'wb') as filehandler:
        pickle.dump(driver.get_cookies(), filehandler)
def load_cookie(driver):
     with open("cookie", 'rb') as cookiesfile:
         cookies = pickle.load(cookiesfile)
         for cookie in cookies:
             print(cookie)
             driver.add_cookie(cookie)

driver = webdriver.Chrome(ChromeDriverManager().install())
url = 'https://www.Youtube.com'
driver.get(url)
#first try to login and generate cookies after that you can use cookies to login eveytime 
load_cookie(driver)
# Do you task here 
save_cookie(driver)
driver.quit()
1

try this, there is a method to add cookie to your driver session

http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.add_cookie

2
  • When do I have to add them in the code? I tried after driver-get() but nothing happens.
    – L'ultimo
    Commented Jul 31, 2017 at 16:58
  • I think the driver needs to be in a page in order to load cookies. Try to get Google.com , then load cookies, then get the target url
    – V-cash
    Commented May 17, 2021 at 17:40
1

I ever met the same issue. Finally I use the chromeoptions to fix this issue instead of cookie file.

    import getpass

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("user-data-dir=C:\\Users\\"+getpass.getuser()+"\\AppData\\Local\\Google\\Chrome\\User Data\\Default")  # this is the directory for the cookies

    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get(url)
3
  • 1
    It is not working for me. It just acts weird. First it will go to google.com and after 5 seconds go to my link, and also didn't logged on automatically... Commented Mar 7, 2020 at 3:21
  • It is also not working for me. I am using Ubuntu and provided following path chrome_options.add_argument("user-data-dir=~/.config/google-chrome/Default/Cookies") but still nothing happening, it demands login Commented Sep 23, 2020 at 5:59
  • Didnt work for me neither...
    – Sergo055
    Commented Jul 31, 2023 at 18:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.