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

Python InvalidArgumentException:消息:无效参数:用户数据目录已在使用错误使用--user data dir使用Selenium启动Chrome,python,selenium,google-chrome,selenium-chromedriver,invalidargumentexception,Python,Selenium,Google Chrome,Selenium Chromedriver,Invalidargumentexception,当我试图使用--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

当我试图使用
--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
如何修复此错误?

此错误消息

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
    
    [12148:21412:0204/035557.731:错误:缓存_util_win.cc(21)]无法移动缓存:访问被拒绝。(0x5)
    [12148:21412:0204/035557.731:错误:cache_util.cc(141)]无法将缓存文件夹C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache移动到C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\ShaderCache\old\u GPUCache\u 000
    [12148:21412:0204/035557.731:错误:disk_cache.cc(178)]无法创建缓存
    [12148:21412:0204/035557.731:错误:着色器\u disk\u cache.cc(605)]着色器缓存创建失败:-2
    在现有浏览器会话中打开。
    回溯(最近一次呼叫最后一次):
    文件“C:\Users\Soma Bhattacharjee\Desktop\Debanjan\PyPrograms\yandex_ru.py”,第18行,在
    driver=webdriver.Chrome(options=options,executable\u path=r'C:\WebDrivers\chromedriver.exe')
    文件“C:\Python\lib\site packages\selenium\webdriver\chrome\webdriver.py”,第81行,在\uuu init中__
    期望的_能力=期望的_能力)
    文件“C:\Python\lib\site packages\selenium\webdriver\remote\webdriver.py”,第157行,在\uuu init中__
    启动会话(功能、浏览器配置文件)
    文件“C:\Python\lib\site packages\selenium\webdriver\remote\webdriver.py”,第252行,在启动会话中
    响应=self.execute(Command.NEW_会话,参数)
    文件“C:\Python\lib\site packages\selenium\webdriver\remote\webdriver.py”,第321行,在execute中
    self.error\u handler.check\u响应(响应)
    文件“C:\Python\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第242行,在check\u响应中
    引发异常类(消息、屏幕、堆栈跟踪)
    selenium.common.exceptions.InvalidArgumentException:消息:无效参数:用户数据目录已在使用,请为--user data dir参数指定唯一值,或不使用--user data dir
    

分析 错误堆栈跟踪明显抱怨访问被拒绝,因为程序无法将缓存文件夹
。\ShaderCache\GPUCache
移动到
。\ShaderCache\old\u GPUCache\u 000
。因此,创建缓存失败,随后创建着色器缓存失败。尽管这些问题引发了
无效argumentException
,但仍然能够在现有的Chrome浏览器会话中强制打开一个新窗口

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


解决方案 你需要注意几件事:

  • 如果您使用默认Chrome配置文件访问同一台测试机器上其他工作的网页,则不应将
    用户数据目录设置为用户数据,因为它仍被您手动启动的其他Chrome进程锁定。
    
    • 在上述场景中,您需要创建并使用另一个Chrome配置文件,您可以在中找到详细的讨论
  • 如果您在独立的测试系统中执行测试,则可以将
    用户数据目录设置为。\user data\Default以访问默认的Chrome配置文件。
    
    • 在上述场景中,您需要创建并使用另一个Chrome配置文件,您可以在中找到详细的讨论
  • 但是,根据最佳实践,您必须始终创建一个新的Chrome配置文件来执行测试,因为默认的Chrome配置文件可能包含扩展、书签、浏览历史记录等,并且可能无法正确加载。
    • 您可以在中找到详细的讨论
此错误消息

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
    
    [12148:21412:0204/035557.731:错误:缓存_util_win.cc(21)]无法移动缓存:访问被拒绝。(0x5)
    [12148:21412:0204/035557.731:错误:cache_util.cc(141)]无法将缓存文件夹C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache移动到C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\ShaderCache\old\u GPUCache\u 000
    [12148:21412:0204/035557.731:错误:disk_cache.cc(178)]无法创建缓存
    [12148:21412:0204/035557.731:错误:着色器\u disk\u cache.cc(605)]着色器缓存创建失败:-2
    在现有浏览器会话中打开。
    回溯(最近一次呼叫最后一次):
    文件“C:\Users\Soma Bhattacharjee\Desktop\Debanjan\PyPrograms\yandex_ru.py”,第18行,在
    driver=webdriver.Chrome(options=options,executable\u path=r'C:\WebDrivers\chromedriver.exe')
    文件“C:\Python\lib\site packages\selenium\webdriver\chrome\webdriver.py”,第81行,在\uuu init中__
    期望的_能力=期望的_能力)
    文件“C:\Python\lib\site packages\selenium\webdriver\remote\webdriver.py”,第157行,在\uuu init中__
    启动会话(功能、浏览器配置文件)
    文件“C:\Python\lib\site packages\selenium\webdriver\remote\webdriver.py”,第252行,在启动会话中
    响应=self.execute(Command.NEW_会话,参数)
    文件“C:\Python\lib\site packages\selenium\webdriver