Home > Blockchain >  Is there any way to load cookies in headless chrome (Selenium)?
Is there any way to load cookies in headless chrome (Selenium)?

Time:09-30

I want to load all the cookies with
options.add_argument('user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data')

but when I try to run this in --headless, it doesn't work, it does not login into the website, is there any way to do this?

What i Have Tried

I am doing this to get my current location using google, I can't send keys to login because it says Browser or app may not be secure

CodePudding user response:

From the selenium docs: https://selenium-python.readthedocs.io/navigating.html#cookies

Before moving to the next section of the tutorial, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for:

Keep in mind managing cookies will get complex. Your cookies can change per interaction with a website, which means your saved cookies will become stale. The solution I have used that works for me is always write out your cookies when you are done with your selenium session.

Here is a very simple version of saving and setting cookies:

import os
import json
import time
from selenium import webdriver

# Assuming the webdriver is next to this file and no chromedriver.exe. I am on linux
CHROME_DRIVER_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chromedriver')

driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)

save_cookies = True
cookies_path = 'valid_json_list_of_your_cookies_path'

# Navigate to the domain that you need to save/write cookies on
driver.get('the_place_you_want_to_login_at')

if save_cookies:
    # Save cookies mode just means we want to save the cookies and do nothing else.
    # Two minutes of sleep to allow you to login to the website
    time.sleep(120)
    # You should be transitioned to the the landing page after logged in by now
    cookies_list = driver.get_cookies()

    # Write out the cookies while you are logged in
    with open(cookies_path, 'w') as file_path:
        json.dump(cookies_list, file_path, indent=2, sort_keys=True)

    driver.quit()

else:
    # Not saving cookies mode assumes we have saved them and can read them in
    with open(cookies_path, 'r') as file_path:
        cookies_list = json.loads(file_path.read())

    # Once on that domain, start adding cookies into the browser
    for cookie in cookies_list:
        # If domain is left in, then in the browser domain gets transformed to f'.{domain}'
        cookie.pop('domain', None)
        driver.add_cookie(cookie)

    # Based on how the page works, it may reload and move you forward after cookies are set
    # OR
    # You may have to call driver.refresh()
  • Related