ChromeDriver headless mode not loading cookies

Viewed 238

Chrome version 89. OS: windows 10. Programming language: c# .net core 3.1

Running in Headed mode, I logged in a site, and the site cookie was properly set. In subsequent runs no login was needed, which is good.

But once I switched to headless mode, the browser was required to enter credentials again. I could supply credentials, however the site also requires a multi-factor auth code that was sent to my cellphone. Even if I entered the auth code in the headless mode, and checked "no auth code for this device" at login, in following runs the auth code was still required.

It seems to me the site cookies in headless mode were not loaded/saved properly.

I'm wondering what caused the discrepancy between head mode and headless mode. Both modes are accessing same chrome profile.

Any help would be appreciated.

            var chromeoptions = new ChromeOptions();
        chromeoptions.AddArguments("--disable-notifications");
        
        chromeoptions.AddArguments("--no-sandbox");

        chromeoptions.AddArguments("--disable-dev-shm-usage");
        chromeoptions.UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss;
        chromeoptions.AddArguments("user-data-dir=" + chromeProfileDir);

        if (!string.IsNullOrEmpty(sHeadless) && sHeadless == "true")
        {
            chromeoptions.AddArguments("--headless");
            chromeoptions.AddArguments("--remote-debugging-port=9222");

        }

        chromeoptions.AddArguments("user-agent=\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36\"");

        chromeoptions.AddArguments("--blink-settings=imagesEnabled=false");

        var chromeDriverService = ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
        chromeDriverService.HideCommandPromptWindow = true;

        ChromeDriver browser = null;
        int nCount = 0;
        while (browser == null && nCount < 3)
        {
            try
            {
                browser = new ChromeDriver(chromeDriverService, chromeoptions, TimeSpan.FromSeconds(180));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error initializing chrome browser: " + ex.Message);
                
            }
            nCount++;
        }

        return browser;
0 Answers
Related