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

Question:

–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:UsersownerDesktopMouseWithoutBordersAutoCheckin.py”, line 15, in
driver = webdriver.Chrome(executable_path=r”C:UsersownerDesktopMouseWithoutBorderschromedriver.exe”, chrome_options=opts)
File “C:UsersownerAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverchromewebdriver.py”, line 75, in init
desired_capabilities=desired_capabilities)
File “C:UsersownerAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py”, line 156, in init
self.start_session(capabilities, browser_profile)
File “C:UsersownerAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py”, line 251, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File “C:UsersownerAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py”, line 320, in execute
self.error_handler.check_response(response)
File “C:UsersownerAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremoteerrorhandler.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:UsersownerAppDataLocalGoogleChrome 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."));
Asked By: Smith

||

Answers:

--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
Answered By: meiskalt7

In my case, this issue was due to ransomware protection that was turned on in Microsoft Defender and was preventing Selenium to access folders in my documents folder. As it was clear that I did nothing wrong in my code, no spaces or special characters whatsoever, I figured out that Windows has something to do with this. When I turned off "Controlled folder access" within the Ransomware protection tab in Windows Defender, the program worked perfectly fine. Might not be your issue, but is worth checking.

Answered By: Dan

In my case the issue was that there was no permission to access to that folder (linux), after chmod and set permissions problem solved.

Answered By: Eitanmg

Given I had the same error, this is what fixed it for me

Replace

options.add_argument("--user-data-dir='C:\Temp\Folder'")

with

options.add_argument("--user-data-dir=C:\Temp\Folder")
Answered By: Zun