Python Selenium Chrome "user data directory is already in use"

3.8k views Asked by At

I'm trying to make a script in python with selenium that will open a website and click a button. Problem is that I have to close chrome before I can run the script otherwise I get this error:

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

Is there a way around this so I don't have to close chrome before running the script everytime?

My code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', chrome_options=options)
driver.get("https://disboard.org/sv/dashboard/servers")

link = driver.find_element_by_link_text("bump")
link.click()
3

There are 3 answers

0
djmonki On

The reason for your issue is because you have added the following argument:

options.add_argument("user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data")

The argument is instructing selenium to use your local chrome profile. Hence, as you have a chrome session open, the automation cannot execute until you close down the browser session.

Remove the argument to execute an independent automation test profile.

See below for the remediated code provided from your question

from selenium import webdriver

PATH = "C:\Program Files (x86)\chromedriver.exe"

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=PATH, options=options)
driver.get("https://disboard.org/sv/dashboard/servers")

link = driver.find_element_by_link_text("bump")
link.click()
1
undetected Selenium On

This error 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 user data directory Default is already in use so ChromeDriver was unable to access the directory and initiate/spawn a new Browsing Context i.e. Chrome Browser session.


Solution

In such cases, you can add/create a new Chrome Profile following the steps below and use it for the AUT (Application Under Test):

  • Open Google Chrome, and select the user icon in the top right and click on Add.

Add New Chrome Profile

  • Enter a name for the new person, select an icon to help represent this new account and make it easier to find later. Then, select Add.

Name_Shortcut_Done

DesktopIcon

  • Additionally, you will find a new sub-directory Profile 1 being created beside Default

Profile1

Now, you can use the Profile 1 sub-directory as follows:

options = Options()
options.add_argument("start-maximized")
options.add_argument("--profile-directory=Profile 1")
options.add_argument("--user-data-dir=C:/Users/user/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(executable_path=r'C:\BrowserDrivers\chromedriver.exe', options=options)
driver.get("https://www.google.com/")

References

You can find a couple of relevant detailed discussions in:

0
Taras On

If it is crucial for your use case that you can use both your Chrome profile and your automation with your profile at the same time, here's a possible solution:

  1. Install a second (older or newer) version of Chrome as described in this thread

  2. Enable synchronization on the desired profile in your current Chrome.

  3. Log in with the desired profile from the second version of Chrome and enable synchronization there as well.

  4. Use the chrome driver compatible with the second version of Chrome in your code.

There may be some problems with synchronization not loading everything that you need, but if it's something simple like saved passwords then this should work.