The best VPN 2024

The Best VPS 2024

The Best C# Book

How to handling authentication window with ChromeDriver?

Do you know how to handling authentication window with ChromeDriver? Today, when I used C# language & Selenium to write a tool to automate the creation of internal git repositories, I happened to encounter the problem of authentication window. If you call this method, it will definitely fail driver.SwitchTo().Alert().SetAuthenticationCredentials, then What is the correct way to do it?

How to handling authentication window with ChromeDriver?
How to handling authentication window with ChromeDriver?

Handling authentication window with ChromeDriver

When you are working in a test environment, Stage or Pre Production, there are cases where you may need to work with applications which are secured with Authentication (Basic Auth).

When ever you enter the URL, it will prompt you to enter the User name and the password and It will not allow to perform any further operations until you provide username and password. And this Authentication pop-up is not a JavaScript pop-up, it is a Browser dialog window which selenium cannot handle simply using sendKeys method which we do for normal JavaScript pop-ups..

How to use C# judge selenium element can be clicked, selected, and visible
How to use C# judge selenium element can be clicked, selected, and visible

To work with Basic Authentication pop-up (which is a browser dialogue window), you just need to send the user name and password along with the application URL.

Syntax:

driver.Navigate().GoToUrl("https://username:password@znlive.com/tfs/ProjectName/Repository/_admin/_versioncontrol");

Check out the example below to execute in Chrome browser:

        /// <summary>
        /// inint chrome webdriver
        /// </summary>
        /// <param name="startUrl">start URL</param>
        /// <returns></returns>
        protected IWebDriver InintWebDriver(string startUrl)
        {
            ChromeDriverService driverService = ChromeDriverService.CreateDefaultService();
            driverService.HideCommandPromptWindow = true;//close cmd window
            ChromeOptions options = new ChromeOptions();
            options.AddExcludedArgument("enable-automation");
            options.AddAdditionalOption("useAutomationExtension", false);
            options.AddArgument("no-sandbox");
            var driver = new ChromeDriver(driverService, options);
            WebDrivers.Add(driver);
            //driver.Navigate().GoToUrl(startUrl);
            driver.Navigate().GoToUrl("https://username:password@znlive.com/tfs/ProjectName/Repository/_admin/_versioncontrol");
            driver.Manage().Window.Maximize();
            return driver;
        }

The only difference, we only need to use the following format to fill in the username and password when passing parameters to the GoToUrl method, so that you can perform automated testing normally.

Firefox, Microsoft Edge browsers are similar, as for IE, I have given up on it.

How to handling authentication window with ChromeDriver?
How to handling authentication window with ChromeDriver?

Leave a Comment