InvalidArgumentException:消息:无效参数:用户数据目录已在使用错误使用 --user-data-dir 启动 Chrome 使用 Selenium [英] InvalidArgumentException: Message: invalid argument: user data directory is already in use error using --user-data-dir to start Chrome using Selenium

查看:113
本文介绍了InvalidArgumentException:消息:无效参数:用户数据目录已在使用错误使用 --user-data-dir 启动 Chrome 使用 Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 --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-packagesseleniumwebdriver
emotewebdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:Program Files (x86)Pythonlibsite-packagesseleniumwebdriver
emoteerrorhandler.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-packagesseleniumwebdriver
    emotewebdriver.py", line 157, in __init__
        self.start_session(capabilities, browser_profile)
      File "C:Pythonlibsite-packagesseleniumwebdriver
    emotewebdriver.py", line 252, in start_session
        response = self.execute(Command.NEW_SESSION, parameters)
      File "C:Pythonlibsite-packagesseleniumwebdriver
    emotewebdriver.py", line 321, in execute
        self.error_handler.check_response(response)
      File "C:Pythonlibsite-packagesseleniumwebdriver
    emoteerrorhandler.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.因此缓存的创建失败,随后着色器缓存创建的创建失败.尽管这些问题会引发 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 进程锁定.
      • 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
        • 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

        这篇关于InvalidArgumentException:消息:无效参数:用户数据目录已在使用错误使用 --user-data-dir 启动 Chrome 使用 Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆