InvalidArgumentException:消息:无效参数:用户数据目录已在使用中,使用--user-data-dir 启动 Chrome 时出现错误,使用 Selenium
- 2025-03-05 09:17:00
- admin 原创
- 115
问题描述:
当我尝试使用--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("");
扫码咨询,免费领取项目管理大礼包!