10

My script I have been writing has been working great. I just added the option so it would open a profile on chrome using this code.

options = webdriver.ChromeOptions
browser = webdriver.Chrome(executable_path=r"C:\Users\princess\AppData\Local\Programs\Python\Python36-32\chromedriver.exe", chrome_options=options)
options.add_argument(r'user-data-dir=C:\Users\princess\AppData\Local\Google\Chrome\User Data')
options.add_argument('--profile-directory=Profile 1')

When used, I get this error code.

C:\Users\Princess\Desktop>CHBO.py
Traceback (most recent call last):
  File "C:\Users\Princess\Desktop\CHBO.py", line 12, in <module>
    browser = webdriver.Chrome(executable_path=r"C:\Users\princess\AppData\Local\Programs\Python\Python36-32\chromedriver.exe", chrome_options=options)
  File "C:\Users\Princess\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 59, in __init__
    desired_capabilities = options.to_capabilities()
TypeError: to_capabilities() missing 1 required positional argument: 'self'

How can I fix this?

John Smith
  • 7,243
  • 6
  • 49
  • 61
Otaku Wiz
  • 193
  • 1
  • 2
  • 11

2 Answers2

15

To create and open a new Chrome Profile you need to follow the following steps :

  • Open Chrome browser, click on the Side Menu and click on Settings on which the url chrome://settings/ opens up.
  • In People section, click on Manage other people on which a popup comes up.
  • Click on ADD PERSON, provide the person name, select an icon, keep the item Create a desktop shortcut for this user checked and click on ADD button.
  • Your new profile gets created.
  • Snapshot of a new profile SeLeNiUm

SeLeNiUm

  • Now a desktop icon will be created as SeLeNiUm - Chrome
  • From the properties of the desktop icon SeLeNiUm - Chrome get the name of the profile directory. e.g. --profile-directory="Profile 2"

profile-directory

  • Get the absolute path of the profile-directory in your system as follows :

    C:\\Users\\Otaku_Wiz\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2
    
  • Now pass the value of profile-directory through an instance of Options with add_argument() method along with key user-data-dir as follows :

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    
  • Execute your Test

  • Observe Chrome gets initialized with the Chrome Profile as SeLeNiUm

SeLeNiUm

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
3

You can use options = Options() or options = webdriver.ChromeOptions() at place of options = webdriver.ChromeOptions

Otherwise you are pointing at an object (namely webdriver.ChromeOptions), and not making an instance of that object by including the needed parenthesis

Reedinationer
  • 5,661
  • 1
  • 12
  • 33
xyz
  • 326
  • 1
  • 12
  • it did open a browser with the profile I wanted but didn't open the sites and navigate as the code is written. I got this back in CMD – Otaku Wiz Mar 14 '18 at 05:21
  • :ERROR:cache_util.cc(134)] Unable to move cache folder C:\Users\princess\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\princess\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000 [30224:11480:0314/012119.198:ERROR:disk_cache.cc(169)] Unable to create cache [30224ERROR:shader_disk_cache.cc(601)] Shader Cache Creation failed: -2 [30224:11480:0314/012119.342:ERROR:browser_gpu_channel_host_factory.cc(120)] Failed to launch GPU process. Created new window in existing browser session. – Otaku Wiz Mar 14 '18 at 05:22
  • Then it says this: – Otaku Wiz Mar 14 '18 at 05:23
  • raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed – Otaku Wiz Mar 14 '18 at 05:23
  • @OtakuWiz this is the answer to your question. the answer to your new question is this part of the error message `Created new window in existing browser session`. You have to close Chrome before running your python code. [see here](https://sqa.stackexchange.com/questions/15311/adding-user-data-dir-option-to-chromedriver-makes-it-not-work-and-timeout-only) –  Jul 10 '18 at 13:40
  • This is correct but poorly formatted and unexplained – Swift Apr 20 '19 at 16:41