Reputation: 312
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")
Upvotes: 0
Views: 9050
Reputation: 86
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"
Upvotes: 2
Reputation: 1943
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")
Upvotes: 3