8

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 command

P.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 use profile-directory argument to give the subdirectory that contains the needed profile.

4
  • 3
    You are actually not using the options variable. You need to add options to chromedriver like this : IWebDriver driver = new ChromeDriver(options );
    – Just code
    Sep 21, 2017 at 10:54
  • Holy ..... thanks lol I dont know how I missed that :D
    – Darkbound
    Sep 21, 2017 at 14:50
  • Right, 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"
    – Darkbound
    Sep 21, 2017 at 14:58
  • It happens :) happy to help
    – Just code
    Sep 21, 2017 at 16:38

4 Answers 4

7

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

5
  • Right, 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"
    – Darkbound
    Sep 21, 2017 at 14:58
  • Yes, chrome has users and when you provide a folder in user-data-dir it creates the profile for Default user in Default folder of the provided folder. So this is a expected behavior Sep 21, 2017 at 15:17
  • Then 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?
    – Darkbound
    Sep 21, 2017 at 15:25
  • 2
    You can, you will need to set profile-directory argument also. So If profile is c:\A\B\C then user-data-dir=C:\A\B and profile-directory=C Sep 21, 2017 at 15:31
  • Thank 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 :)
    – Darkbound
    Sep 21, 2017 at 15:38
0

If you are starting with the profile of the browser on the computer you are looking for, you can

  1. Open normal google chrome and go to ('chrome://version') enter link description here

  2. Copy the Profile Path but take all of the "Data" folder and copy it to where the program is running

  3. C# Coding:

    https://rextester.com/INK23784

By creating a folder named "profile" where the program is running, you can add all the profile information, plugins, and so on. etc. We have copied the data folder in everything and when opening the browser "ChromeOptions" to selenium your profile files, etc. that's everything

0

You can try this code: (It worked for me)

string path_profile = @"D:\PROJECT_XMARKETING_4.0\Profiles\1";
// string path_profile = @"D:\PROJECT_XMARKETING_4.0\Profiles2\2";
IWebDriver _webDriver;
ChromeDriverService cService = ChromeDriverService.CreateDefaultService();
cService.HideCommandPromptWindow = true;
_webDriver = new ChromeDriver(cService);
_webDriver.Manage().Cookies.DeleteAllCookies();
ChromeOptions options = new ChromeOptions();
options.AddArgument($"user-data-dir={path_profile}");
_webDriver = new ChromeDriver(cService, options);
//_webDriver.Navigate().GoToUrl("https://phamtani.com/");
//_webDriver.Navigate().GoToUrl("https://alink.vn/");
//_webDriver.Navigate().GoToUrl("http://api.hostip.info/get_json.php");
1
  • 1
    @phamtani Please provide some explanation. Jun 30, 2020 at 14:53
0

Set user-data-dir to C:\Users[your-username]\AppData\Local\Google\Chrome\User Data

Full Code:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

private IWebDriver _driver { set; get; }

public YourConstructor()
{
    _driver = CreateBrowserDriver();
}

private IWebDriver CreateBrowserDriver()
{
    try
    {
        var options = new ChromeOptions();
        options.AddArgument("test-type");
        options.AddArgument("--ignore-certificate-errors");
        options.AddArgument("no-sandbox");
        options.AddArgument("disable-infobars");
        //options.AddArgument("--headless"); //hide browser
        options.AddArgument("--start-maximized");
        //options.AddArgument("--window-size=1100,300");
        //options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);

        // Profile [Change:User name]
        options.AddArgument(@"user-data-dir=C:\Users\Haddad\AppData\Local\Google\Chrome\User Data");

        var service = ChromeDriverService.CreateDefaultService();
        service.HideCommandPromptWindow = true;
        service.SuppressInitialDiagnosticInformation = true;

        return new ChromeDriver(service, options);
    }
    catch
    {
        throw new Exception("Error: Chrome is not installed.");
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.