0

when I run this code with chrome already open 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

I need to have multiple profiles open at the same time, what can I do?

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

options = Options()
options.add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("--profile-directory=Profile 3")
driver = webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options)

driver.get("https://www.instagram.com")
petezurich
  • 9,280
  • 9
  • 43
  • 57
CastoldiG
  • 178
  • 3
  • 17
  • Does this answer your question? [Python -- Opening multiple tabs using Selenium](https://stackoverflow.com/questions/39281806/python-opening-multiple-tabs-using-selenium) – Martin Solhaug Standal Mar 25 '21 at 19:14
  • A profile is associated one-to-one per selenium web driver. You should instantiate multiple selenium driver per profile – tbhaxor Mar 25 '21 at 20:09
  • @tbhaxor thanks for the answer, I tried to create 2 folders with chromedriver inside, and put the 2 paths inside the codes, but it gives me the same error ... what exactly do you mean by instantiate multiple selenium driver per profile? – CastoldiG Mar 26 '21 at 08:19

2 Answers2

3

Let's say you want to open two chrome profiles

You need to instantiate two web drivers with the profile you want to set.

From instantiate I meant, you need to create two chrome web drivers because once the options are set and you have created the driver, you cannot change this later

So,

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

options = [Options(), Options()]
options[0].add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data")
options[1].add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data")

options[0].add_argument("--profile-directory=Profile 3")
options[1].add_argument("--profile-directory=Profile 4") # add another profile path

drivers = [webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options[0]), webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options[1])]

drivers[0].get("https://instagram.com")
drivers[1].get("https://instagram.com")
tbhaxor
  • 1,659
  • 2
  • 13
  • 43
2

The user-data directory get locked by the first instance and the second instance fails with exception So the solution is to create a Userdata for each profile

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--user-data-dir=C:\\anyPathWhereYouWantToSaveYourUserData\\profile1")
driver = webdriver.Chrome(executable_path=r'C:\Windows\System32\chromedriver.exe', options=options)
driver.get("https://www.instagram.com")

No worries you can access your new profiles now manually if you would like to just by creating shortcuts you just need to add to your target in your shortcut

"C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\anyPathWhereYouWantToSaveYourUserData\profile1"