Python Selenium –user-data-dir option ERROR: could not remove old devtools port file

chromiumpythonpython-3.xseleniumselenium-chromedriver

–user-data-dir option

I tried to open Chromedriver with –user-data-dir option but following error comes out. I've tried this in many ways for 1 month and still don't have clue. Please help me!

Error message is:

Traceback (most recent call last):
File "C:\Users\owner\Desktop\MouseWithoutBorders\AutoCheckin.py", line 15, in
driver = webdriver.Chrome(executable_path=r"C:\Users\owner\Desktop\MouseWithoutBorders\chromedriver.exe", chrome_options=opts)
File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in init
desired_capabilities=desired_capabilities)
File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 156, in init
self.start_session(capabilities, browser_profile)
File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 251, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\owner\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\owner\AppData\Local\Programs\Python\Python37\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\owner\AppData\Local\Google\Chrome\ is still attached to a running Chrome or Chromium process.
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17738 >x86_64)

and My python test code is:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from myidez import ID, PW

opts = webdriver.ChromeOptions()
opts.add_argument('--start-maximized')
opts.add_argument('--headless')
opts.add_argument('--no-sandbox')
opts.add_argument('--disable-dev-shm-usage')
opts.add_argument('--disable-gpu')
opts.add_argument("--user-data-dir= C:\\Users\\owner\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path=r"./chromedriver.exe", chrome_options=opts)

I have found this Error comes from chromium code
references:
https://chromium.googlesource.com/chromium/src/+/master/chrome/test/chromedriver/chrome_launcher.cc
https://peter.sh/experiments/chromium-command-line-switches/

Status RemoveOldDevToolsActivePortFile(const base::FilePath& user_data_dir) {
  base::FilePath port_filepath = user_data_dir.Append(kDevToolsActivePort);
  // Note that calling DeleteFile on a path that doesn't exist returns True.
  if (base::DeleteFile(port_filepath, false)) {
    return Status(kOk);
  }
  return Status(
      kUnknownError,
      std::string("Could not remove old devtools port file. Perhaps "
                  "the given user-data-dir at ") +
          user_data_dir.AsUTF8Unsafe() +
          std::string(" is still attached to a running Chrome or Chromium "
                      "process."));

Best Answer

--user-data-dir= C:\\Users\\owner\\AppData\\Local\\Google\\Chrome\\User Data

You have this string, it's wrong, I had the same problem when I have used double quotes or spaces in path, remove them. Error "Could not remove old devtools port file" says that Chrome get invalid path from you in this case. After removing spaces:

--user-data-dir=C:\\Users\\owner\\AppData\\Local\\Google\\Chrome\\User Data
Related Topic