void_eater
void_eater

Reputation: 173

How to use certain Chrome profile with selenium python?

It's been 6 years since it was reported first: https://github.com/SeleniumHQ/selenium/issues/854

From here https://chromedriver.chromium.org/getting-started I try this code:

import time
from selenium import webdriver

driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(60) # Let the user actually see something!
driver.quit()

While it's launched go chrome://version/ and see:

Profile Path C:\Users\USERCU~1\AppData\Local\Temp\scoped_dir13280_930640861\Default

To set up certain profile I tried the code from here How to load default profile in Chrome using Python Selenium Webdriver?

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Users\\user123\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 16") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

But instead of using specified path it actually creates profile inside another profile's path so chrome://version shows:

Profile Path C:\Users\usercuhuh\AppData\Local\Google\Chrome\User Data\Profile 16\Default

So the problem is that \Default folder is automatically added to specified user-data-dir. How can I bypass it and launch actually profile 16?

Upvotes: 2

Views: 14049

Answers (1)

Hana Bzh
Hana Bzh

Reputation: 2260

First of all


update your ChromeDriver to the latest version


(This part is mandatory). Then pass two arguments --profile-directory=Profile 1 and user-data-dir=C:\\Users\\user123\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 16 to the Chrome binary

options = Options()
options.add_argument('--profile-directory=Profile 16')
options.add_argument("user-data-dir=C:\\Users\\Hana\\AppData\\Local\\Google\\Chrome\\User Data\\") #Path to your chrome profile
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", options=options)

Note that in user-data-dir you should only pass the path of all profiles then the name of specific profile (Here, Profile 16) should be passed through --profile-directory

You can check out full source code named PythonChromeProfile in SeleniumWebDriver-Tiny_projects on Github, Which I tested successfuly. Also you can read about creating Chrome profiles and find the path to that profile Here

Upvotes: 6

Related Questions