16

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?

L'ultimo
  • 521
  • 1
  • 5
  • 17
  • 1
    I think you could save the cookies to a file and then load it – 0xMH Jul 31 '17 at 13:31
  • Does this answer your question? [How to save and load cookies using Python + Selenium WebDriver](https://stackoverflow.com/questions/15058462/how-to-save-and-load-cookies-using-python-selenium-webdriver) – user202729 Aug 18 '21 at 05:23
  • check this [how to use cookie in selenium](https://stackoverflow.com/questions/63399459/how-the-save-the-browser-sessions-in-selenium/72740726#72740726) – shakib Jun 24 '22 at 08:40

6 Answers6

25

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)
Florent Roques
  • 2,424
  • 2
  • 20
  • 23
Arount
  • 9,853
  • 1
  • 30
  • 43
  • 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 Jul 31 '17 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 Jul 31 '17 at 19:11
  • @Arount, what do mean by "1. go home page"? Thanks. – Ratmir Asanov Jan 05 '18 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 Jan 07 '18 at 20:29
  • @Arount, so, do you mean driver.get(URL)? I tried to load my saved cookies and update the page after this action, but no luck. I don't understand why. – Ratmir Asanov Jan 07 '18 at 20:54
  • 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 Dec 21 '18 at 13:09
  • I would advise in using json format, because the cookies are inherently dictionaries and lists: https://stackoverflow.com/a/61750071/8500357 – JulianWgs May 12 '20 at 10:58
10

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)
JulianWgs
  • 961
  • 1
  • 14
  • 25
  • 2
    Also, as estated in the Python documentation (https://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 Feb 15 '22 at 19:32
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
Hardik Sondagar
  • 4,347
  • 3
  • 28
  • 48
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

Satish
  • 1,976
  • 1
  • 15
  • 19
  • When do I have to add them in the code? I tried after *driver-get()* but nothing happens. – L'ultimo Jul 31 '17 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 May 17 '21 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)
Shi Harold
  • 27
  • 1
  • 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... – Abhay Salvi Mar 07 '20 at 03: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 – Usama Tahir Sep 23 '20 at 05:59
  • Didnt work for me neither... – Sergo055 Jul 31 '23 at 18:35