C# Selenium Start Chrome with Different User Profile
You din't use the options objects at all.
IWebDriver driver = new ChromeDriver();
Should be
IWebDriver driver = new ChromeDriver(options);
Edit-1 - Chrome profiles and users
Chrome has User data directory for storing profiles. Inside this directory multiple profiles can be maintained. There are two arguments that can be used
- user-data-directory
- profile-directory
If only user-data-directory
is specified then a Default
directory inside the same would be used. If profile-directory
is specified then that directory inside the user-data-directory
is used
Darkbound
Updated on June 28, 2022Comments
-
Darkbound almost 2 years
For the past 2 days, I've been trying to find a way to start Chrome with a different profile but to no avail. No matter what I do, the profile that Selenium loads for chrome is always some temporary profile like "C:\Users\DARKBO~1\AppData\Local\Temp\scoped_dir14308_25046\Default"
I have tried the following code:
ChromeOptions options = new ChromeOptions(); options.AddArgument(@"user-data-dir=C:\SeleniumProfiles\Default"); IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("chrome://version");
First I tried using the directories for the profiles directly from the Chrome folder, didn't work. Then I created a new folder and moved the profiles there, I've tried doing this both in C:\ and in D:\ . No difference whatsoever. I've tried running the user-data-dir argument both like it currently is in the code and with -- in front of it. I've tried using double backslashes without the @ symbol, still nothing. No matter what I do the profile directory is always the Selenium temp directory.
P.S. The current C:\SeleniumProfiles directory I created through the command prompt using the
chrome user-data-dir=C:\SeleniumProfiles
commandP.S. 2: My mistake was very simple, I forgot to put the options in the constructor of the new driver. And as Tarun made it clear,
user-data-dir
only gives Chrome the directory that contains the profiles, then we need to useprofile-directory
argument to give the subdirectory that contains the needed profile. -
Darkbound almost 7 yearsRight, now its working, but whatever directory for a profile I give it, it creates another Default directory inside the directory that I specified, and its not using the settings from the Profile that I specified. So when I give it "C:\SeleniumProfiles\Default", the profile path in chrome://version becomes "C:\SeleniumProfiles\Default\Default"
-
Tarun Lalwani almost 7 yearsYes, chrome has users and when you provide a folder in
user-data-dir
it creates the profile forDefault
user inDefault
folder of the provided folder. So this is a expected behavior -
Darkbound almost 7 yearsThen what should I do to make it use the profile that is in the XXXXXX folder, instead of it making a new Default folder, within the XXXXXX folder?
-
Tarun Lalwani almost 7 yearsYou can, you will need to set
profile-directory
argument also. So If profile isc:\A\B\C
thenuser-data-dir=C:\A\B
andprofile-directory=C
-
Darkbound almost 7 yearsThank you very much Tarun! This is what I needed, I didn't know that I need a second argument! So basicly, user-data-dir specifies the main directory with all profiles, and profile-directory specifies the subdirectory that contains the wanted profile :)
-
Peter Csala almost 4 years@phamtani Please provide some explanation.