</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

How do you handle HTTP Proxy Authentication pop-ups in the browser?

Pass credentials directly in the URL (http://username:password@site.com) or use ChromeOptions/FirefoxProfile to configure proxy authentication.

Answer

Handling HTTP Proxy Authentication Pop-ups

HTTP Proxy Authentication pop-ups appear when accessing sites that require credentials at the proxy/server level before the page loads.

Method 1 — Embed credentials in URL (simplest):

Java
// Format: http://UserName:Password@Example.com
driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth");

// Real example
driver.get("https://admin:admin@the-internet.herokuapp.com/basic_auth");

Method 2 — WebDriverWait + Alert.authenticateUsing():

Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("username", "password"));

Method 3 — Firefox Profile pre-configured:

Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);

FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);

Method 4 — Chrome extension approach:

Java
// Programmatically create a Chrome extension that handles auth
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("proxy_auth_extension.crx"));
WebDriver driver = new ChromeDriver(options);

Method 5 — DevTools Protocol (Chrome):

Java
ChromeDriver chromeDriver = (ChromeDriver) driver;
Map<String, Object> headers = new HashMap<>();
headers.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("user:pass".getBytes()));
chromeDriver.executeCdpCommand("Network.setExtraHTTPHeaders",
    Collections.singletonMap("headers", headers));

URL credential format:

CODE
http://UserName:Password@Example.com
https://admin:secretpass@internal.company.com/app

Key point: Form authentication URL by embedding username and password directly in the URL. Example:

  • http://the-internet.herokuapp.com/basic_auth (no credentials — prompts popup)
  • https://admin:admin@the-internet.herokuapp.com/basic_auth (bypasses popup)

Follow AutomateQA

Related Topics