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

2025-03-05 09:17:00
admin
原创
114
摘要:问题描述:当我尝试使用--user-data-dir当前用户使用 Selenium 启动 Chrome 时收到以下错误: File "C:Program Files (x86)Pythonlibsite-packagesseleniumwebdriver emotewebdriver.py&quo...

问题描述:

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

  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

我该如何修复这个错误?


解决方案 1:

此错误信息...

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 浏览器user data directory会话,因为它已经在使用中。

此错误可以通过以下方式重现:

  • 代码块:

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/")
  • 完成相关回溯:

[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。因此创建缓存失败,随后创建Shader Cache 创建失败。虽然这些问题引发了问题,但强制能够在现有的Chrome 浏览器会话InvalidArgumentException中打开一个新窗口。

尽管抛出了错误,但新的 Chrome 窗口仍然会启动,但仍与已打开的Chrome会话相连,但新窗口无法由WebDriver实例控制。因此,您会data:,在 URL 栏中看到。


解决方案

您需要注意以下几点:

  • 如果您使用默认 Chrome 配置文件访问同一台测试机器上的其他工作网页,则不应将其设置user-data-dir用户数据**,因为它仍然被您手动启动的其他 Chrome 进程锁定。

+ 在上述场景中,你需要创建并使用另一个*Chrome 配置文件,你可以在*如何通过 Python 打开 Chrome 配置文件中找到详细讨论
  • 如果您在独立的测试系统中执行测试,则可以设置user-data-dir..\User Data\Default来访问默认的 Chrome 配置文件

+ 在上述场景中,您需要创建并使用另一个*Chrome 配置文件,您可以在*如何在 Selenium Webdriver Python 3 中使用 Chrome 配置文件中找到详细讨论
  • 但是,根据最佳实践,您必须始终创建新的Chrome 配置文件来执行测试,因为默认 Chrome 配置文件可能包含扩展程序书签浏览历史记录等,并且可能无法正确加载。

+ 您可以在如何通过 Selenium 的 --user-data-dir 参数打开 Chrome 配置文件中找到详细讨论

解决方案 2:

最简单、最容易的修复方法是清除现有的已打开的 chrome 驱动程序:步骤如下:在任务栏的搜索窗口中的 Spotlight 搜索中输入任务管理器,或使用其他方式访问任务管理器。当任务管理器向导/窗口弹出时,搜索 chromedriver,右键单击它,然后单击“结束任务”。就是这样。这不是一个永久的解决方案。一旦你多次打开 chrome 浏览器,你就必须执行相同的步骤来避免这个问题。希望这对我有所帮助,因为我正在寻找一个稳定的解决方案。

解决方案 3:

在尝试跳过登录过程时遇到了同样的问题,解决方案是关闭已经打开的浏览器。

解决方案 4:

正如Tes 的回答所提到的,我打开了 Windows 任务管理器并关闭了所有 chrome.exe 和 chromedriver.exe 进程,它就成功了!!!

我使用这些配置通过我的 Chrome 配置文件打开 Google Chrome:

options = webdriver.ChromeOptions()
    options.add_argument('user-data-dir=C:\\Users\\my_user\\AppData\\Local\\Google\\Chrome\\User Data')
    driver = webdriver.Chrome(executable_path='./chromedriver.exe', options=options)

解决方案 5:

System.setProperty("webdriver.chrome.driver", "C://Users//Mhamed//Desktop//chromedriver.exe");

               ChromeOptions options = new ChromeOptions();

                                options.setExperimentalOption("useAutomationExtension", false);

                      WebDriver        driver = new ChromeDriver(options);

                



        

  driver.manage().window().fullscreen();

         driver.get("");
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   3975  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   2742  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Freshdesk、ClickUp、nTask、Hubstaff、Plutio、Productive、Targa、Bonsai、Wrike。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在项目管理过程中面临着诸多痛点,如任务分配不...
项目管理系统   80  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Monday、TeamGantt、Filestage、Chanty、Visor、Smartsheet、Productive、Quire、Planview。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时,常...
开源项目管理工具   88  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Smartsheet、GanttPRO、Backlog、Visor、ResourceGuru、Productive、Xebrio、Hive、Quire。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在选择项目管理工具时常常面临困惑:...
项目管理系统   77  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用