Selenium C# – Browser Authentication Popup

Selenium C# – Browser Authentication Popup
Introduction
Dealing with browser authentication popups is something we often encounter in test automation. These popups show up when you try to access protected resources that require a username and password. With Selenium, we can skip this step by putting the credentials right into the URL. This guide will walk you through how to handle browser authentication popups using Selenium with C#.
Code Implementation
Below is a C# Selenium script that demonstrates how to automate browser authentication using URL-based credential embedding.
Example: Handling Browser Authentication Popups in Selenium C
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System.Globalization;
namespace SACSSAuto.QA.SeleniumPractice
{
[TestClass]
public class Selenium_80
{
/****
* Sample Code Definition :- Selenium - Browser Authentication Popup
* Developed By :- Pankaj Kumar
* Date of Creation :- 31-Jan-2025
* Project :- SACSSAUTO - C# [Web / Mobile / API] *
*/
[TestMethod]
public void Selenium_Test()
{
Console.WriteLine("Selenium - Browser Authentication Popup");
// Initialize Chrome WebDriver
IWebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.Manage().Window.Maximize();
// Set the username and password
string username = "admin";
string password = "admin";
// Open the URL with authentication credentials
string URL = $"https://{username}:{password}@the-internet.herokuapp.com/basic_auth";
driver.Navigate().GoToUrl(URL);
// Wait for 5 seconds
Thread.Sleep(5000);
// Get the title of the page
string title = driver.Title;
Console.WriteLine("The page title is " + title);
// Verify the text on the page
string text = driver.FindElement(By.TagName("p")).Text;
Console.WriteLine("The text present on the page is ==> " + text);
// Wait for 5 seconds
Thread.Sleep(5000);
// Quit the driver
driver.Quit();
}
}
}
Steps Explained
1. Initialization
Launch Chrome browser using WebDriver.
Maximize the browser window for better visibility.
2. Authentication via URL
- Embed the username and password in the URL using the format: https://username:password@domain.com
- Navigate to the URL.
3. Verification
Retrieve and print the page title.
Locate and print the paragraph text to confirm successful login.
4. Cleanup
Wait for a few seconds.
Close the browser session.
Expected Outcome
Automatically authenticates using the provided credentials.
Successfully loads the protected page.
Prints the page title and text for verification.
Closes the browser.
Conclusion
Selenium makes it super easy to handle browser authentication popups by letting you embed credentials right into the URL. This approach simplifies testing protected resources, so you don't have to fuss with those native browser dialogs.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
