Python Selenium --用户数据目录选项错误:无法删除旧的devtools端口文件

pxiryf3j  于 2022-12-26  发布在  Python
关注(0)|答案(4)|浏览(217)

--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.cchttps://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."));
xvw2m8pv

xvw2m8pv1#

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

你有这个字符串,它是错误的,我有同样的问题,当我在路径中使用双引号或空格,删除他们.错误“无法删除旧devtools端口文件”说,Chrome从你这里得到无效的路径在这种情况下.删除空格后:

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

uwopmtnx2#

在我的情况下,问题是没有权限访问该文件夹(linux),在chmod和设置权限问题解决后。

bis0qfac

bis0qfac3#

在我的情况下,这个问题是由于勒索软件的保护是在微软防御打开,并阻止 selenium 访问我的文档文件夹中的文件夹。因为很明显,我没有做错我的代码,没有空格或特殊字符,无论如何,我想出了Windows与此有关。当我关闭“控制文件夹访问”在勒索软件保护标签中的Windows防御,程序运行得非常好。可能不是你的问题,但值得检查。

smdnsysy

smdnsysy4#

考虑到我有同样的错误,这就是为我修复它的方法
替换

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

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

相关问题