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

查看:2115
本文介绍了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)\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.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

如何解决此错误?

推荐答案

此错误消息...

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 无法使用已指定的user data directory来启动新的 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.

此错误可以重现如下:

  • 代码块:

  • 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\{}\AppData\Local\Google\Chrome\User Data".format(getpass.getuser()))
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.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:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\ShaderCache\old_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:\Users\Soma Bhattacharjee\Desktop\Debanjan\PyPrograms\yandex_ru.py", line 18, in <module>
        driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      File "C:\Python\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
        desired_capabilities=desired_capabilities)
      File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
        self.start_session(capabilities, browser_profile)
      File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
        response = self.execute(Command.NEW_SESSION, parameters)
      File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
        self.error_handler.check_response(response)
      File "C:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.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
    

  • 错误堆栈跟踪明显抱怨访问被拒绝,因为程序无法将缓存文件夹..\ShaderCache\GPUCache移动到..\ShaderCache\old_GPUCache_000.因此,缓存的创建失败,随后 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 ..\ShaderCache\GPUCache to ..\ShaderCache\old_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 实例.因此,您会在网址栏中看到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.

    您需要注意以下几点:

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