selenium.common.exceptions.InvalidArgumentException:消息:参数无效:用户数据目录已在使用真实 Chrome 浏览器时出错

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use error with real Chrome Browser

我试图在我的真实 Chrome 中打开网站,而不是驱动程序但是我得到了错误列表并且它没有打开 url** 这是我的代码

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

s = Service("C:\Users\Administrator\Desktop\chromedriver.exe")

options = webdriver.ChromeOptions() 
options.add_argument("--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data")
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(service=s, chrome_options=options)

driver.get("https://www.instagram.com")

错误如:

  C:\Users\Administrator\Desktop\Py Sele\Sele.py:11: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome(service=s, chrome_options=options)
Opening in existing browser session.
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\Py Sele\Sele.py", line 11, in <module>
    driver = webdriver.Chrome(service=s, chrome_options=options)
  File "C:\Program Files\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "C:\Program Files\Python39\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 93, in __init__
    RemoteWebDriver.__init__(
  File "C:\Program Files\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 268, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Program Files\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 359, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Program Files\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
Stacktrace:
Backtrace:

这个错误信息...

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir

...表示 --user-data-dir 已在使用中。

您的主要问题是您已经打开了一个 Google Chrome 浏览器会话供您手动浏览。

您的代码块中没有此类错误。但是,您似乎正在将程序作为 Administrator 执行。但是,

A common cause for Chrome to crash during startup is running Chrome as root user (administrator) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.


解决方案

在您以用户身份执行测试之前,例如管理员,确保您没有使用同一用户打开任何 Chrome 浏览器 windows,即 管理员