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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With