Ever since the announcement from Google that it will be shipping Headless Chrome, a lot of people have turned to using python to drive Chrome. This may be attributed to the fact that headless browsers have no Graphical User interface (GUI), as well as being navigated using a command line interface or a program. Many, however turn to driving Chrome using python due to the fact that it’s much more fun.
To drive Chrome through Python, however, a lot of people face obstacles like opening a Chrome profile through Python which is required before any other action can be taken. Here, we will discuss the steps to follow on how to open a Chrome profile through Python.
Step by Step Guide to Open Chrome Profile through Python
- Launch your chrome browser, then click open chrome settings where chrome://settings/ is displayed
- Go to People section, select Manage Other people
- Click on Add Person
- Input your name and pick one of numerous icons displayed
- You have to Check the ‘Create a desktop shortcut for this user’ item
- Click the Add button.
- Your new profile is successfully created
This is a sample of how your newly opened profile should look like:
- You will find a desktop icon tagged SeLeNiUm – Chrome
- Through the properties of the SeLeNiUm – Chrome, get the name of the profile directory. e.g. –profile-directory=”Profile 2″. An example of this is shown below:
- Obtain the absolute path of the profile-directory in your computer using the following:
C:\\Users\\Otaku_Wiz\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2
- Then pass the value of profile-directory through an instance of Options with add_argument() method along with key user-data-dir as shown below:
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")
- Launch your Test and you will notice your Chrome is initiated with the Chrome profile as Selenium