3

I'm trying to load cookies that already exist (they are in C:\Users\nicoc\AppData\Local\Google\Chrome\User Data\Default\Cookie) into a Selenium driver instance.

Now, I've read the fact that I can load cookies with the function driver.get_cookies() and save them with pickle but I don't want to launch a Selenium driver session and save the cookies, I need to load the already existing ones in some ways and attach them to a new Selenium driver session with something like:

cookies = pickle.load(cookiesfile)
         for cookie in cookies:
             driver.add_cookie(cookie)

I've also checked this answer https://stackoverflow.com/a/63158404/7848740 where he loads a .pkl file containing the cookies. Unfortunately, Google Chrome cookies have no format and, opening with a text editor, seems to be an SQLite3 database

I've also tried browsercookie https://pypi.org/project/browsercookie/ but it's not compatible with the latest Chrome versions

4
  • i don't know that they need to be a pickle, couldn't you save them as a simple json, then read them in and assign each key:value? Commented Oct 6, 2021 at 14:37
  • @SuperStew the issue is that the file already exist so I'll probably need to load and open in someway and save it again in a format that driver.add_cookie(cookie) read it but, I don't understand how to read the original Cookie file Commented Oct 6, 2021 at 14:42
  • 1
    oh, if reading the pickle file is the issue, look up the docs for the pickle module. Should be lots of info Commented Oct 6, 2021 at 14:44
  • @SuperStew no, no, I know how to read a pickle. The problem is how to read the Cookie file inside the Chrome folder as it has no extension Commented Oct 6, 2021 at 16:55

1 Answer 1

2

What I did which works is use the browser_cookie3 library to grab the cookies from the normal browser sessions and use dict_from_cookiejar from the requests library to turn the cookie jar produced into a dictionary that can be processed into something that we can send to the Selenium webdriver instance.

from urllib.parse import urlparse
import browser_cookie3
from requests.utils import dict_from_cookiejar

src_url = "https://a.b.c.com/homepage"
base_url = urlparse(src_url).netloc
cookies = browser_cookie3.chrome(domain_name=f'.{base_url}')
cookies_dict = dict_from_cookiejar(cookies)
driver.get(src_url)
for c_name, c_value in cookies_dict.items():
    driver.add_cookie({'name': c_name, 'value': c_value})
driver.get(src_url)

I would think that it should be possible to write this in a better way at a couple of places at least; but I couldn't figure out anything better. For instance, I would have thought that we should be able to establish what our URL is before reading it to avoid loading it twice (if you try to add the cookies before reading it gives an error about a domain mismatch so you'll need to read the web page, overwrite the cookies & read the web page again). Also, it seems a bit awkward for there to be no way for Selenium to consume the cookie jar created by browser_cookie3 directly.

However, this seems to work to accomplish what the OP is describing (for instance, if you are logged in on your browser, the browser instance used by Selenium should also be logged in after you "steal" the main browser instance's cookies for it as described above).

In the sample code, change cookies = browser_cookie3.chrome(domain_name=f'.{base_url}') to the appropriate method for your browser of choice, of course.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.