open multiple chrome profile with selenium

Question:

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:Developmentchromedriver.exe', options=options)

driver.get("https://www.instagram.com")

Asked By: CastoldiG

||

Answers:

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:Developmentchromedriver.exe', options=options[0]), webdriver.Chrome(executable_path=r'C:Developmentchromedriver.exe', options=options[1])]

drivers[0].get("https://instagram.com")
drivers[1].get("https://instagram.com")
Answered By: tbhaxor

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:WindowsSystem32chromedriver.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 FilesGoogleChromeApplicationchrome.exe" --user-data-dir="C:anyPathWhereYouWantToSaveYourUserDataprofile1"
Answered By: thesalah1212
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.