How to open a Chrome Profile through –user-data-dir argument of Selenium

As per your code trials you were trying to load the Default Chrome Profile which will be against all the best practices as the Default Chrome Profile may contain either of the following:

  • Extensions
  • Bookmarks
  • Browsing History
  • etc

So the Default Chrome Profile may not be in compliance with you Test Specification and may raise exception while loading. Hence you should always use a customized Chrome Profile as below.

To create and open a new Chrome Profile you need to follow the following steps :

  • Open Chrome browser, click on the Side Menu and click on Settings on which the url chrome://settings/ opens up.
  • In People section, click on Manage other people on which a popup comes up.
  • Click on ADD PERSON, provide the person name, select an icon, keep the item Create a desktop shortcut for this user checked and click on ADD button.
  • Your new profile gets created.
  • Snapshot of a new profile SeLeNiUm

SeLeNiUm

  • Now a desktop icon will be created as SeLeNiUm – Chrome
  • From the properties of the desktop icon SeLeNiUm – Chrome get the name of the profile directory. e.g. –profile-directory=”Profile 2″

profile-directory

  • Get the absolute path of the profile-directory in your system as follows :

    C:\\Users\\Thranor\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2
    
  • Now pass the value of profile-directory through an instance of ChromeOptions with AddArgument method along with key user-data-dir as follows :

    m_Options = new ChromeOptions();
    m_Options.AddArgument("--user-data-dir=C:/Users/Me/AppData/Local/Google/Chrome/User Data/Profile 2");
    m_Options.AddArgument("--disable-extensions");
    m_Driver = new ChromeDriver(@"pathtoexe", m_Options);
    m_Driver.Navigate().GoToUrl("somesite");
    
  • Execute your Test

  • Observe Chrome gets initialized with the Chrome Profile as SeLeNiUm

SeLeNiUm

Leave a Comment