InvalidArgumentException:消息:无效参数:用户数据目录已在使用错误使用 --user-data-dir 使用 Selenium 启动 Chrome

问题描述

当我尝试使用 --user-data-dir 让当前用户使用 Selenium 启动 Chrome 时,我收到以下错误:

When I am trying to use --user-data-dir for the current user to start Chrome using Selenium I am getting an error as:

  File "C:Program Files (x86)Pythonlibsite-packagesseleniumwebdriveremotewebdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:Program Files (x86)Pythonlibsite-packagesseleniumwebdriveremoteerrorhandler.py", line 242, 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

我该如何解决这个错误?

How can I fix this error?


解决方案

这个错误信息...

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

...暗示 ChromeDriver 无法使用指定的 用户数据目录 启动新的 Chrome 浏览器 会话,因为它已经正在使用中.

...implies that the ChromeDriver was unable to initiate the new Chrome Browser session using the specified user data directory as it was already in use.

这个错误可以重现如下:

This error can be reproduced as follows:

  • 代码块:

  • Code Block:

from selenium import webdriver
import getpass

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument(r"--user-data-dir=C:Users{}AppDataLocalGoogleChromeUser Data".format(getpass.getuser()))
driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')
driver.get("https://www.google.com/")

  • 完成相关回溯:

  • Complete relevant traceback:

    [12148:21412:0204/035557.731:ERROR:cache_util_win.cc(21)] Unable to move the cache: Access is denied. (0x5)
    [12148:21412:0204/035557.731:ERROR:cache_util.cc(141)] Unable to move cache folder C:UsersSoma BhattacharjeeAppDataLocalGoogleChromeUser DataShaderCacheGPUCache to C:UsersSoma BhattacharjeeAppDataLocalGoogleChromeUser DataShaderCacheold_GPUCache_000
    [12148:21412:0204/035557.731:ERROR:disk_cache.cc(178)] Unable to create cache
    [12148:21412:0204/035557.731:ERROR:shader_disk_cache.cc(605)] Shader Cache Creation failed: -2
    Opening in existing browser session.
    Traceback (most recent call last):
      File "C:UsersSoma BhattacharjeeDesktopDebanjanPyProgramsyandex_ru.py", line 18, in <module>
        driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')
      File "C:Pythonlibsite-packagesseleniumwebdriverchromewebdriver.py", line 81, in __init__
        desired_capabilities=desired_capabilities)
      File "C:Pythonlibsite-packagesseleniumwebdriveremotewebdriver.py", line 157, in __init__
        self.start_session(capabilities, browser_profile)
      File "C:Pythonlibsite-packagesseleniumwebdriveremotewebdriver.py", line 252, in start_session
        response = self.execute(Command.NEW_SESSION, parameters)
      File "C:Pythonlibsite-packagesseleniumwebdriveremotewebdriver.py", line 321, in execute
        self.error_handler.check_response(response)
      File "C:Pythonlibsite-packagesseleniumwebdriveremoteerrorhandler.py", line 242, 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
    

  • 错误堆栈跟踪清楚地抱怨 访问被拒绝,因为程序无法将缓存文件夹 ..ShaderCacheGPUCache 移动到 ..ShaderCacheold_GPUCache_000.因此 cache 创建失败,随后 Shader Cache Creation 创建失败.虽然这些问题会引发 InvalidArgumentException,但可以在现有 Chrome 浏览器会话中强制打开一个新窗口.

    The error stack trace clearly complains of Access is denied as the program was unable to move the cache folder ..ShaderCacheGPUCache to ..ShaderCacheold_GPUCache_000. hence the creation of cache failed and subsequently creation of Shader Cache Creation failed. Though these issues raises the InvalidArgumentException but forcefully able to open a new window within the existing Chrome Browser Session.

    虽然抛出了错误,但新的 Chrome 窗口仍会启动,但仍与已打开的 Chrome 会话连接,但新窗口无法由 WebDriver 实例.因此,您会在 url 栏中看到 data:,.

    Though the error is thrown, still the new Chrome window gets initiated but remains attached with the already opened Chrome session but the new window can't be controlled by the WebDriver instance. Hence you see data:, in the url bar.

    你需要注意几件事:

    • 如果您使用 默认 Chrome 配置文件在同一台测试机上访问其他工作的网页,则不应设置 user-data-dir 作为用户数据,因为它仍然被您手动启动的其他 Chrome 进程锁定.
      • 在上述场景中,您需要创建和使用另一个 Chrome 配置文件,您可以在 如何通过 Python 打开 Chrome 配置文件
      • If you are using the Default Chrome Profile to access webpages for your other work on the same Test Machine, you shouldn't set user-data-dir as the User Data as it remains locked by the other Chrome process you have initiated manually.
        • In the above scenario you need to create and use another Chrome Profile and you can find a detailed discussion in How to open a Chrome Profile through Python
        • 在上述场景中,您需要创建和使用另一个 Chrome 配置文件,您可以在 如何在 Selenium Webdriver Python 3 中使用 Chrome 配置文件
        • In the above scenario you need to create and use another Chrome Profile and you can find a detailed discussion in How to use Chrome Profile in Selenium Webdriver Python 3
        • 您可以在 如何通过 Selenium 的 --user-data-dir 参数打开 Chrome 配置文件

    相关文章