0

I want to use selenium with my Google profile, I created this code:

options = webdriver.ChromeOptions() 

options.add_argument(r"user-data-dir=C:\Users\myprofile\AppData\Local\Google\Chrome\User Data") #Path to my chrome profile

driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe", options=options)

driver.get(url)

I got this error:

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

I also tried another profile, but return the same error:

options = webdriver.ChromeOptions() 

options.add_argument('--user-data-dir=C:/Users/mxs/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')

driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe", options=options)

driver.get(url)

also, I tried codes in both Jupyter Notebook and VSCode

Saif Siddiqui
  • 856
  • 1
  • 13
  • 33
khaled koubaa
  • 836
  • 3
  • 14
  • I usually copy `User Data` of my Chrome where I'm logged in to sites in a seperate folder and pass that in as parameter to the ChromeDriver. – Christian Baumann Oct 08 '20 at 14:45
  • this happens when another instance of Chrome is open using the same data. Not sure if google will let a bot login though... – pcalkins Oct 08 '20 at 19:53
  • does these links work for your case? (https://stackoverflow.com/questions/59987080/invalidargumentexception-message-invalid-argument-user-data-directory-is-alre) (https://stackoverflow.com/questions/50635087/how-to-open-a-chrome-profile-through-user-data-dir-argument-of-selenium/50637211#50637211) the second link is used for c# but the logic is similar. – liamsuma Oct 13 '20 at 20:35

2 Answers2

0

Suggestion 1

If you are okay with only one selenium process running at a time, you can use queue to achieve it. As long as you use only one process to access user-dir, it'll not cause any error.

Suggestion 2

If you are trying to stay logged in for some website, you can reuse cookies. In this case you don't need to use user-data-dir thing. Selenium will automatically create temporary user-data-dir on every run.

Suggestion 3

If you still want to use the same profile for every session, here is the hack to do it. You keep original --user-data-dir unused, instead you copy all the content of your original profile directory to some temporary directory and use it with --user-data-dir every time you initiate chrome selenium.

Hardik Sondagar
  • 4,347
  • 3
  • 28
  • 48
0

Suggestion

When you are trying to run your python script close all the chrome windows. I got the error

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

when I had one of the chrome windows open. So it is better to close all the chrome windows and try again.

Pro Chess
  • 831
  • 1
  • 8
  • 23