0

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?

void_eater
  • 113
  • 1
  • 3
  • 9

1 Answers1

3

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

Hana Bzh
  • 2,212
  • 3
  • 18
  • 38
  • 1
    This: 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 – void_eater Sep 20 '21 at 10:17
  • Thank you! I also installed selenium 4.0.0rc1 as it implements CDP (and my purpose is https://stackoverflow.com/questions/69243355/how-to-fetch-specific-request-which-happened-when-i-open-an-url-in-a-webbrowser) But its documentation is empty yet: https://www.selenium.dev/documentation/support_packages/chrome_devtools/ May be you can suggest where I find it? – void_eater Sep 20 '21 at 10:27
  • I just know that you create a new thread. let me take a look – Hana Bzh Sep 20 '21 at 10:38
  • And one more thing I noticed: if you going to use for example profile 1 and profile 16 at the same time you would get error: selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir and only launch specified Profile but do no more. So you should put desired profiles' folders you gonna use into different --user-data-dir – void_eater Sep 20 '21 at 10:45
  • I have two profiles in `user-data-dir` path, and just call only profile 1 in `--profile-directory`. No Error raised for me – Hana Bzh Sep 20 '21 at 10:48
  • I got this error when I had profile 1 Chrome window launched and my code tried to use profile 16. And when I closed profile 1 Chrome window code runs without problems. – void_eater Sep 20 '21 at 11:12