Search code examples
c#seleniummicrosoft-edgebrowser-automationselenium-edgedriver

Selenium Can't save the session EdgeDriver c#


i use this code to open edge browser or chrome browser :

 int ProcessID = -1;
 public enum BrowserType
    {
        Chrome=1,
        Edge=2
    }
 public void OpenChrome(
       string Website,
       int TimeToWaitInMinutes,
       string FolderPathToStoreSession,
       out int ChromeProcessID)
        {

            try
            {

                //chrome process id
                int ProcessID = -1;

                //time to wait until open chrome
                var TimeToWait = TimeSpan.FromSeconds(TimeToWaitInMinutes);

                if (browserValue == BrowserType.Chrome)
                {
                    ChromeDriverService cService = ChromeDriverService.CreateDefaultService();
                    //hide dos screen
                    cService.HideCommandPromptWindow = true;
                    ChromeOptions options = new ChromeOptions();
                    options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
                    options.AddArguments("chrome.switches", "--disable-extensions");
                    driver_Ref = new ChromeDriver(cService, options, TimeToWait);
                    ProcessID = cService.ProcessId;
                }
                else
                {

                    EdgeDriverService cService = EdgeDriverService.CreateDefaultService(".", "msedgedriver.exe");
                    //hide dos screen
                    cService.HideCommandPromptWindow = true;

                    EdgeOptions options = new EdgeOptions();


                    options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
                    driver_Ref = new EdgeDriver(cService, options, TimeToWait);
                    ProcessID = cService.ProcessId;
                }
                driver_Ref.Navigate().GoToUrl(Website);

                ChromeProcessID = ProcessID;

            }
            catch (Exception ex)
            {
                if (driver_Ref != null)
                {
                    try
                    {
                        driver_Ref.Close();
                        driver_Ref.Quit();
                        driver_Ref.Dispose();
                    }
                    catch { }
                }

                driver_Ref = null;

                ChromeProcessID = -1;
                throw ex;
            }
        }

i use:

  • Microsoft.Edge.SeleniumTools 3.141.3
  • Selenium.Support 3.141.0
  • Selenium.WebDriver 3.141.0
  • Selenium.WebDriver.MSEdgeDriver 105.0.1343.27
  • Microsoft Edge Version 105.0.1343.42 (Official build) (64-bit)

the code is worked fine when i use chrome browser and seesion saved, i mean the login and other browser data are saved to FolderPathToStoreSession, FolderPathToStoreSession is found on application data ,but when i use edge browser session not saved , no error happened, but login and other browser data not saved ,this code not working:

options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);

if you notice that the only difference between code chrome and code edge is :

options.AddArguments("chrome.switches", "--disable-extensions");

please help me to fix this problem.


Solution

  • i have to change :

    EdgeDriverService cService = EdgeDriverService.CreateDefaultService(".", "msedgedriver.exe");
    

    to :

    EdgeDriverService cService = EdgeDriverService.CreateChromiumService(".", "msedgedriver.exe");
    

    and add :

    options.UseChromium = true;
    

    and it worked fine.