71

I'd like to launch Chrome with its default profile using Python's webdriver so that cookies and site preferences persist across sessions.

How can I do that?

idontknow
  • 438
  • 5
  • 16
MadRabbit
  • 2,460
  • 2
  • 17
  • 18

6 Answers6

125

This is what finally got it working for me.

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

To find path to your chrome profile data you need to type chrome://version/ into address bar . For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default, to use it in the script I had to exclude \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.

Also if you want to have separate profile just for selenium: replace the path with any other path and if it doesn't exist on start up chrome will create new profile and directory for it.

MadRabbit
  • 2,460
  • 2
  • 17
  • 18
  • 9
    Thanks, I could not find answer for this question for several hours, removing the Default from path finally works. – user3281831 Apr 24 '17 at 18:44
  • I am following this but still, Chrome doesn't load the profile. I am using WSL (windows subsystem for linux) and using the windows browser. – BND Feb 19 '20 at 05:47
  • it opens default profile, What if i want to open a specific profile? – Abdul Ahad Mar 28 '20 at 21:32
  • 2
    This was working for me only when my chrome was closed. Had to fix this by creating a copy of the profile directory and giving its path instead of the original path. Otherwise it was giving directory under use if another chrome was already open with the same profile. – anandhu Jun 19 '20 at 07:00
  • 2
    Removing `/Default/` from path finally worked. but now i am facing issue, when add cookies, chrome instance opens which already have my google account signed in, but it fails with exception, do not work – Usama Tahir Sep 23 '20 at 06:51
  • What's the firefox version of getting your default user profile loaded? – fractal Feb 12 '21 at 01:06
  • After using this my chrome opens always in incognito mode, and lost my google account? Please, how to revert that? – Skittles Jan 10 '22 at 19:24
  • Can I open chrome profile with my account in Selenium ? – rehan qasim Jul 22 '23 at 19:09
  • Thank you! This code also works well with webdriver_manager, as shown below: from webdriver_manager.chrome import ChromeDriverManager ff_driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options = options) – KBurchfiel Aug 02 '23 at 19:39
19

This solved my problem. (remove Default at the end)

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/username/.config/google-chrome")

cls.driver = webdriver.Chrome(options=options,
                              executable_path="./../ext/chromedriver")

Chrome_Options ist deprecated. Use options instead

n1k31t4
  • 2,745
  • 2
  • 24
  • 38
Nils Zenker
  • 659
  • 7
  • 11
  • There is something missing in your first line: `from selenium import [...?]` – Jundiaius Apr 25 '18 at 12:11
  • 1
    I tried this but it didn't work for me. @NilsZenker can you please look at my question at https://stackoverflow.com/questions/53589103/selenium-chrome-driver-cant-set-user-data-dir?noredirect=1#comment94043600_53589103? – etayluz Dec 03 '18 at 17:15
  • have a similar problem and follow your instruction but receive error message: selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome unexpectedly exited. Status code was: 0 – Victor Feb 03 '20 at 00:13
16

I solved my problem with answer of "Yoannes Geissler".

In my case My profile was named "Profile 2"

My Code :

options = webdriver.ChromeOptions() 

options.add_argument('--user-data-dir=C:/Users/GOD/AppData/Local/Google/Chrome/User Data')

options.add_argument('--profile-directory=Profile 2')

wd = webdriver.Chrome(options=options)

The below line solved my problem:

options.add_argument('--profile-directory=Profile 2')
Everest Ok
  • 191
  • 2
  • 5
  • Worked for me without the line `options.add_argument('--profile-directory=Profile 2')`. Anyway, I guess it'll be different for some. Or it could be because you have more than one profile. – Ecks Dee Jun 08 '21 at 23:54
  • I confirm that this one works very well and logically on my Macbook Mojave Pycharm... The mechanism is simple as that: - The first line (user-data-dir) is directory of big folder 'Chrome' - The second line (profile-directory) is name of your Profile. I think Selenium officially state this somewhere but no one in this post found it. – Zui Zui Aug 07 '23 at 17:06
5

Just to share what worked for me. Using default's profile was complicated, chrome keeps crashing.

from pathlib import Path
from selenium import webdriver

driver_path = Path("{}/driver/chromedriver75.exe".format(PATH_TO_FOLDER))
user_data_dir = Path("{}/driver/User Data".format(PATH_TO_FOLDER))

options = webdriver.ChromeOptions()

# TELL WHERE IS THE DATA DIR
options.add_argument("--user-data-dir={}".format(user_data_dir))

# USE THIS IF YOU NEED TO HAVE MULTIPLE PROFILES
options.add_argument('--profile-directory=Default')

driver = webdriver.Chrome(executable_path=driver_path, options=options)

driver.get("https://google.com/")

By doing this Chrome will create the folder User Data and keep all the data in it where I want and it's easy to just move your project to another machine.

Yoannes Geissler
  • 791
  • 1
  • 9
  • 18
  • I have a similar problem and follow your instruction but receive error message: selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome unexpectedly exited. Status code was: 0 – Victor Feb 03 '20 at 00:12
5

This answer is pretty simple and self-explained.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

exec_path_chrome = "path/to/Google Chrome" #Do not use this path that is extracted from "chrome://version/"
exec_path_driver = "path/to/chromedriver"

ch_options = Options() #Chrome Options
ch_options.add_argument("user-data-dir = /path/to/Chrome Profile") #Extract this path from "chrome://version/"

driver = webdriver.Chrome(executable_path = exec_path_driver, options = ch_options) #Chrome_Options is deprecated. So we use options instead.

driver.get("https://stackoverflow.com/a/57894065/4061346")

As @MadRabbit said type chrome://version/ into the address bar to find the path to your chrome profile data.

  • It appears like this in Windows C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
  • It appears like this in Mac /Users/user/Library/Application Support/Google/Chrome/Default

So all you have to do is to erase the last portion Default from the profile path.

Note: Make sure you don't run more than one session at the same time to avoid problems.

Joe
  • 879
  • 3
  • 14
  • 37
  • have a similar problem and follow your instruction but receive error message: selenium.common.exceptions.WebDriverException: Message: Service /Applications/Google Chrome.app/Contents/MacOS/Google Chrome unexpectedly exited. Status code was: 0 – Victor Feb 03 '20 at 00:12
  • @Victor check these links https://stackoverflow.com/a/39998437/4061346 https://stackoverflow.com/a/46027522/4061346 and tell me which one fixed your problem. – Joe Feb 11 '20 at 09:46
  • Hi Youssof H, Many thanks for replying me, but I follow your advice and add the path for the chrome binary, still, the profile doesn't load. I have the following two questions asked with a bit more detail of my problem, would you mind to take a look please? https://stackoverflow.com/questions/60048743/selenium-with-firefor-or-chrome-profile and https://stackoverflow.com/questions/60031070/webdriverexception-error-when-using-selenium-chrome-webdriver-with-options – Victor Feb 15 '20 at 23:48
  • What do u mean by delete the last portion `Default` from the profile path? – Luk Aron Jul 12 '20 at 08:27
1

This is what I did.

import chromedriver_binary
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument(r"--user-data-dir=User Data Directory")
chrome_options.add_argument(r"--profile-directory=Profile name")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable 
logging"])
chrome_options.add_experimental_option("excludeSwitches", ["enable 
automation"])
chrome_options.add_argument("start-maximized")
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.wait = WebDriverWait(self.driver, 20)

Here we do not need to give the chrome driver path.

user13973948
  • 72
  • 1
  • 7