Selenium will not load my default Chrome Profile and I cannot figure out why. I have tried both Profile 1 and a Default profile with the same error (below). I have confirmed with Task Manager that all Chrome windows are shut down before running this code. Any thoughts?
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import os
os.system("taskkill /f /im geckodriver.exe /T")
os.system("taskkill /f /im chromedriver.exe /T")
os.system("taskkill /f /im IEDriverServer.exe /T")
os.system("taskkill /f /im chrome.exe /T")
driver2 = r"C:\Users\xxx\.wdm\drivers\chromedriver\87.0.4280.20\win32\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\xxx\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")
driver = webdriver.Chrome(executable_path=driver2, chrome_options=options)
driver.get("https://www.google.co.in")
Traceback (most recent call last):
File "C:\Users\xxx\OneDrive\Python\pyReportRun.py", line 16, in <module>
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
File "C:\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at C:\Users\xxx\AppData\Local\Google\Chrome\User Data\Profile 1 is still attached to a running Chrome or Chromium process
This error message...
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session as the
user-data-dir
which you have passed an an argument is still attached to a running Chrome Browsing Context.Details
Using Default Chrome Profile for Test Automation will be against all the best practices as the Default Chrome Profile may contain either/all of the following:
So the Default Chrome Profile may not be in compliance with you Test Specification and may occasionally raise exceptions while trying to load. Hence you should always use a customized Chrome Profile.
If your usecase still warrants to use the Default Chrome Profile you need to ensure that all the google-chrome, chromium or selenium-chromedriver are stopped/killed and you can follow the below mentioned details.
Location of Default Chrome Profile
As per the documentation in How to Find Your Chrome Profile Folder on Windows, Mac, and Linux the location for Chrome’s default profile folder differs depending on your platform. The locations are:
C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
Users/<username>/Library/Application Support/Google/Chrome/Default
/home/<username>/.config/google-chrome/default
You need to replace
<username>
with the name of your user folder. The default profile folder is simply named Default (or default in Linux). However, if you’ve created additional profiles, their folder names are not as obvious. The name you assigned to the profile when you created it displays on a name button on the right side of the title bar on the Chrome window. Unfortunately, the name Chrome uses on the associated profile folder is a generic, numbered name likeProfile 3
.If you need to know any of the Chrome Profile's folder name, you simply need to access
chrome://version
in the address bar and press Enter.Snapshot:
The
Profile Path
shows the location of the current profile. For example, the location of my Default profile in my Windows 10 system isC:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\Default
. You can select the path and copy it and paste it into File Explorer in Windows, the Finder on OS X or into a file manager like Nautilus in Linux to access that folder.Sample Code (Windows 10)
Finally, to access the Default Chrome Profile you can use the following Python based solution: