Selenium chrome remote webdriver cookies

Viewed 165

I've looked over numerous questions and the selenium docs, explaining how to force Chrome to honor cookie data from pre-existing profiles. I've set Chrome settings to allow all cookies, and I tried to use Python pickling to persist cookie data across sessions. Still, I'm getting a guest or Profile 1 session instead of the signed-in session that I'm looking for. Note that I'm using an implementation that must use selenium remote, as I need it to work off of a server/client relationship rather than locally. On my old environment, Firefox profiles were much easier to implement.. I'm on a Linux/Ubuntu (Jammy) w/latest Chrome setup..

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import numpy as np
import pandas as pd
import pickle
import re

fp = webdriver.ChromeOptions()
fp.add_argument("home/dharkko/.config/google-chrome/default")
fp.add_argument('--headless')
fp.add_argument("--profile-directory=Profile 1")
#fp.add_cookie({"name":"hash_name","value":"hash_value"})
#fp.add_argument("user-data-dir=selenium")

driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    options=fp
)

pickle.dump(driver.get_cookies(), open("/home/dharkko/.config/google-chrome/Default/cookies.pkl","wb"))

 
cookies = pickle.load(open("/home/dharkko/.config/google-chrome/Default/cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
1 Answers

As you are using the default Chrome Profile you need to pass the user-data-dir parameter as follows:

fp.add_argument("user-data-dir=/path/to/Google/Chrome/User Data")

Additionally, incase is installed in the default location you can remove the following line:

fp.add_argument("home/dharkko/.config/google-chrome/default")
Related