Selenium – Point Towards Default Chrome Session

cookiesgoogle-chromeseleniumselenium-webdriversession-cookies

Though I realize it's NOT "good" practice – I have a use case where I need to point (hook up) the Selenium driver to my default Chrome session/profile.

My default profile is here:
~/Library/Caches/Google/Chrome/Default

Here is how I'm seting it up currently: (not working)

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Caches/Google/Chrome")
options.add_argument("--profile-directory=Default")
browser = webdriver.Chrome(options=options, executable_path=r"./chromedriver")
browser.get("http://google.com")

I'm using Chrome version 74.0.3729.169 and chromedriver version ChromeDriver 74.0.3729.6 (which is the compatible version).

When Chrome opens I don't see any cookies in Chrome's settings so it's clear it's NOT being pointed to my default session. Also, I see that a Selenium directory has been created (which appears to mean that it has failed to connect to the session at ~/Library/Caches/Google/Chrome/Default.

How do I hook up selenium to my default Chrome session? This is the same session as one sees when normally opening up Chrome.

I've looked at this other question, but the answer there fails to address how to point Selenium towards default session. Also – it's an outdated question – Chrome and Chromedriver have progressed a lot since then. Also, the question there assumes that the poster is able to connect to default session – I am not able to do that which suggests that the Chromedriver/Chrome have changed since then. Also that question is for Windows – I'm on a Mac where things work differently.

Best Answer

Make sure you are pointing to the right folder using "Chrome://version".

enter image description here

I am using the windows but it should be similar in you mac case too.

Refer to this link for more information.

How to create a custom profile:

You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the user-data-dir switch set to some new directory. If the path doesn't exist, Chrome will create a new profile in the specified location. You can then modify the profile settings as desired, and ChromeDriver can use the profile in the future. Open chrome://version in the browser to see what profile Chrome is using.

Reference:

http://chromedriver.chromium.org/capabilities

Related Question