InvalidArgumentException: Message: invalid argument: user data directory is already in use error using --user-data-dir to start Chrome using Selenium
Asked Answered
K

5

23

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

How can I fix this error?

Kingship answered 30/1, 2020 at 13:24 Comment(6)
Could You describe your example more clearly ??? Provide what you tried, what is used version? Why not try example from chromedriver page and check if it works ??Pauper
Please check here on how to ask questions on stackoverflow: stackoverflow.com/help/how-to-askLap
Please add the code you are using to open the browser and go to the page.Unattached
import logging from selenium import webdriver driver = webdriver.Chrome() def test_3_2(): logging.debug("running test_3_2") driver.get("docs.python.org/") logging.info("URL: " + driver.current_url) logging.info("Title: " + driver.title) assert "Documentation" in driver.title driver.quit() if name == "main": test_3_2()Kingship
I have added the code I am running aboveKingship
This error is showing up because you are running Chrome with that user while you have a Chrome Window open already with that user. It will probably work when you close Chrome or use a different user.Ushijima
S
21

This error message...

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

...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.

This error can be reproduced as follows:

  • 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
    

Analysis

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.

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.


Solution

You need to take care of a couple of things:

  • If you are using the Default Chrome Profile to access webpages for your other work on the same Test Machine, you shouldn't set user-data-dir as the User Data as it remains locked by the other Chrome process you have initiated manually.
  • If you are executing your tests in a isolated test system, you can set user-data-dir as ..\User Data\Default to access the Default Chrome Profile.
  • However as per best practices you must always create a new Chrome Profile to execute your tests as the Default Chrome Profile may contain Extensions, Bookmarks, Browsing History, etc, and may not load properly.
Stanislaus answered 3/2, 2020 at 23:29 Comment(3)
But I need my existing session of the browser to stay. While I carry forward with a Selenium test with the same user profile. How could you do that?Trojan
@Trojan Let's discuss in details within Selenium Chat RoomStanislaus
@undetectedSelenium how do you get the full traceback to see which files are being accessed?Peterec
A
5

The simplest and easiest fix is; to clear existing opened chrome driver: Here're the steps: type task manager to the Spotlight Search/in the Search Window at the task bar or using other ways accessing to task manager. When the Task Manager Wizard/window pops up, search chromedriver, right click on it and then click "End Task". That's it. It's not an eternal fix. Once you opened chrome browser multiple times, you got to do the same step avoid the issue. Hope this helps as am looking for a stable fix.

Antineutrino answered 12/5, 2020 at 19:46 Comment(0)
R
2

Just had the same problem trying to skip the login process and the solution was to close the already openned browser.

Regardant answered 12/6, 2021 at 23:5 Comment(0)
R
1

As Tes answer mentioned, I opened up the Windows Task Manager and I closed all the chrome.exe and chromedriver.exe processes and it worked!!!

I used these configurations to open Google Chrome using my Chrome Profile:

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)
Roselynroseman answered 15/7, 2020 at 7:47 Comment(1)
do you know how this would work on a macbook? /Users/user_name/Library/Application Support/Google/Chrome/Default didn't workPeriphery
S
-4

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("");
Saddleback answered 20/1, 2021 at 1:29 Comment(1)
Please answer in a neat & tidy manner. make sure you use the code tag properly and add a simple explanation.Bialy

© 2022 - 2024 — McMap. All rights reserved.