How to open a Chrome Profile through Python

Posted on

Solving problem is about exposing yourself to as many situations as possible like How to open a Chrome Profile through Python and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How to open a Chrome Profile through Python, which can be followed any time. Take easy to follow this discuss.

How to open a Chrome Profile through Python

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:UsersprincessAppDataLocalProgramsPythonPython36-32chromedriver.exe", chrome_options=options)
options.add_argument(r'user-data-dir=C:UsersprincessAppDataLocalGoogleChromeUser Data')
options.add_argument('--profile-directory=Profile 1')

When used, I get this error code.

C:UsersPrincessDesktop>CHBO.py
Traceback (most recent call last):
  File "C:UsersPrincessDesktopCHBO.py", line 12, in <module>
    browser = webdriver.Chrome(executable_path=r"C:UsersprincessAppDataLocalProgramsPythonPython36-32chromedriver.exe", chrome_options=options)
  File "C:UsersPrincessAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverchromewebdriver.py", line 59, in __init__
    desired_capabilities = options.to_capabilities()
TypeError: to_capabilities() missing 1 required positional argument: 'self'

How can I fix this?

Answer #1:

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:pathtochromedriver.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

Answered By: DebanjanB

Answer #2:

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

Answered By: xyz
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published. Required fields are marked *