MadRabbit
MadRabbit

Reputation: 2520

How to load default profile in Chrome using Python Selenium Webdriver?

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?

Upvotes: 72

Views: 155107

Answers (6)

Nils Zenker
Nils Zenker

Reputation: 709

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 is currently deprecated in selenium. Use options instead

Upvotes: 20

Everest Ok
Everest Ok

Reputation: 271

I solved my problem with the 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')

Upvotes: 24

user13973948
user13973948

Reputation: 72

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.

Upvotes: 1

Youssof
Youssof

Reputation: 1001

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.

Upvotes: 6

Yoannes Geissler
Yoannes Geissler

Reputation: 841

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.

Upvotes: 6

MadRabbit
MadRabbit

Reputation: 2520

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.

Upvotes: 128

Related Questions