Python – Newly created chrome profile won’t load in

pythonpython-3.xseleniumselenium-webdriver

When I run this code

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument(r"user-data-dir=C:\Users\micha\AppData\Local\Google\Chrome\User Data\Profile 1")

driver = webdriver.Chrome(executable_path=r'C:\Users\micha\Desktop\Visual_projects\chromedriver.exe', chrome_options = chrome_options)

driver.get("https://store.steampowered.com/")

This error pops up :
[12216:1336:0411/232857.718:ERROR:browser_switcher_service.cc(238)] XXX Init()

Could someone please help me. I don't know what is wrong but the program won't open the new profile I created. Any help would be appreciated.

I searched everywhere how to fix this error but I think the guides are outdated

Best Answer

Not exactly answer to your question. But I found this link very useful.

Also, I see you are trying to include user dir options. Actually, this is not required since it creates a temporary directory while launching chromedriver.exe.

To know options for chromedriver.exe - chromedriver.exe -h is of great help.

See example below which works great at my end. Also, I prefer to use paths without spaces in windows, it helps keep things simple. If you use --verbose instead of --log-level=INFO you will get all logs.

In the chromedriver log, you can see the default arguments given to the chromedriver.exe.

import time, os

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

exePath = './driver/chromedriver.exe'
logPath = os.path.join(os.getcwd(),'logs','chromedriver.log')
serviceArgs = ["--log-level=INFO", "--readable-timestamp", "--append-log"]

# service = Service(executable_path=exePath, log_path=logPath, service_args=serviceArgs)
service = Service(log_path=logPath, service_args=serviceArgs)
service.start()
print(service.service_url)
print(service.process.pid)

# driver = webdriver.Remote(service.service_url)
# Update to remove infobars and save password popups

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
prefs = {"profile.password_manager_enabled": False, "credentials_enable_service": False}
options.add_experimental_option("prefs", prefs)
caps = options.to_capabilities()

driver = webdriver.Remote(service.service_url, desired_capabilities=caps)

# driver = webdriver.Remote('http://localhost:63404')
driver.get('http://www.google.com/')
driver.maximize_window()
time.sleep(3) # Let the user actually see something!
driver.get("https://github.com")
time.sleep(3)
driver.back()
time.sleep(3)
driver.close()
driver.quit()
service.stop()

service.stop() terminates the chromedriver.exe or else it will keep running in the background.
Folder structure at my end is like below:

rootDir
--driver/chromedriver.exe
--testchromedriver.py (with above code)

Also, created as killchromedriver.py file to terminate all chromedriver.exe instances in case they are running in background.

import psutil # pip install psutil

for process in psutil.process_iter():
    if(process.name() == 'chromedriver.exe'):
        process.terminate()
        print('chromedriver.exe was running and is now terminated')

Update: Adding options to remove the infobars and save password popups

Related Topic