当前位置: 首页 > 知识库问答 >
问题:

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

孙恩
2023-03-14

当我试图使用--user data dir让当前用户使用Selenium启动Chrome时,我得到一个错误,如下所示:

  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

如何修复此错误?

共有3个答案

桓高澹
2023-03-14

正如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)
尤飞尘
2023-03-14

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

米浩穰
2023-03-14

此错误消息...

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

这个错误可以复制如下:

>

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/")

完成相关回溯:

[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\u GPUCache\u 000。因此,缓存创建失败,随后着色器缓存创建失败。尽管这些问题引发了无效argumentException,但仍然能够在现有的Chrome浏览器会话中强制打开一个新窗口。

虽然抛出了错误,但新的Chrome窗口仍会启动,但仍与已打开的Chrome会话保持连接,但新窗口无法由WebDriver实例控制。因此,您可以在url栏中看到数据:,

你需要注意几件事:

  • 如果您使用默认Chrome配置文件访问您在同一台测试机器上的其他工作的网页,您不应该将user-data-dir设置为用户数据,因为它仍然被另一个锁定Chrome您手动启动的进程。
    • 在上述场景中,您需要创建和使用另一个ChromeProfile,您可以在如何通过Python打开ChromeProfile中找到详细讨论
    • 在上面的场景中,您需要创建并使用另一个Chrome配置文件,您可以在Selenium Webdriver Python 3中找到关于如何使用Chrome配置文件的详细讨论
    • 您可以通过Selenium的--user data dir参数找到关于如何打开Chrome配置文件的详细讨论

 类似资料:
  • 我正在尝试用python和selenium制作一个脚本,它将打开一个网站并单击一个按钮。问题是我必须先关闭chrome才能运行脚本,否则会出现以下错误: 有没有办法避免每次运行脚本之前都关闭chrome? 我的代码:

  • 我正在尝试使用以下命令单独使用硒启动chrome 但是,当我触发我的网页驱动测试铬时,它会暂时打开铬,它崩溃并出现以下错误 { " trace id ":" faca 75 a3 db 657 eed 4b 96 bdac 93977746 "," eventTime": 1637343152240678600," eventName": "exception "," attributes ":{

  • 我正在使用Symfony Panther和Laravel框架。我用这个时出错了 未知错误:无法为用户数据目录创建临时目录{"异常":"[对象](Facebook\WebDriver\Exception\Unknown nErrorException(代码:0): 我已经安装了适用于Windows 7.4.2的XAMPP。 PHP:7.4.2,Apache:2.4.41 创作者json [2020-

  • 问题内容: 下面的简单测试案例失败了,但有一个例外。 我不知道怎么了 问题答案: Mockito要求您在存入方法调用时仅使用原始值或仅使用匹配器。完整的例外情况(您未在此处发布)肯定可以解释所有情况。 简单更改行: 至 它应该工作。

  • 在这里,我发布了我的控制台错误: 在点击Tomcat的stop后,我在控制台中得到以下错误:

  • 我无法启动我的tomcat服务器。我几乎什么都试过了。切换位置没有帮助,作为服务器位置。我在Windows8上。 在这里我发布了我的控制台错误: 在点击Tomcat的stop后,我在控制台中收到以下错误: