Proxy authentication in Selenium becomes difficult when the browser—not the webpage—asks for a username and password. The right solution depends on the proxy type, browser, Selenium version, and whether the credentials can be supplied outside the test. This guide explains reliable Selenium 4 approaches, where older workarounds still fit, and why alert-handling code often fails.

What is a Proxy?

A proxy is an intermediary that sends requests to websites on behalf of your browser. The destination sees the proxy's IP address instead of the machine running Selenium. Testing teams use proxies to validate regional experiences, route traffic through controlled networks, inspect requests, or distribute automated workloads.

A proxy URL commonly contains a host and port, such as proxy.example.test:8080. Private services may also require a username and password. Selenium can configure the proxy endpoint through browser options, but authentication prompts are controlled by the browser and are not always exposed to WebDriver like an ordinary HTML form.

Choose a suitable network: static addresses are convenient for allowlisting, while rotating networks suit large-scale collection. Our residential proxy providers guide explains the practical differences.

Use automation only where you have authorization, respect website terms, and keep credentials out of source code and test logs.

Difference between SOCKS and HTTP Proxy

HTTP proxies understand web traffic and are commonly configured with separate HTTP and HTTPS values. SOCKS proxies operate at a lower level and can carry different kinds of TCP traffic without interpreting the application protocol. SOCKS5 additionally supports authentication and is usually the preferred SOCKS version.

CharacteristicHTTP/HTTPS proxySOCKS5 proxy
TrafficDesigned for browser web requestsWorks with multiple TCP-based applications
Protocol awarenessUnderstands HTTP requests and headersForwards traffic without reading HTTP semantics
Selenium setupSet HTTP and SSL proxy fieldsSet the SOCKS endpoint and version
Typical useWeb testing, filtering, caching, inspectionFlexible tunneling and non-HTTP traffic

The browser and proxy provider must support the same method. A valid endpoint does not guarantee that Selenium can complete a native username/password dialog automatically.

How to Handle Proxy in Selenium Webdriver in Chrome

For a proxy that does not require interactive authentication, Selenium 4 can assign a Proxy object to ChromeOptions. The current official Selenium browser-options documentation demonstrates this capability-based approach.

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

Proxy proxy = new Proxy();
proxy.setHttpProxy("proxy.example.test:8080");
proxy.setSslProxy("proxy.example.test:8080");

ChromeOptions options = new ChromeOptions();
options.setProxy(proxy);

WebDriver driver = new ChromeDriver(options);
driver.get("https://example.com");

For SOCKS5, set the SOCKS endpoint and version instead:

Proxy proxy = new Proxy();
proxy.setSocksProxy("proxy.example.test:1080");
proxy.setSocksVersion(5);

ChromeOptions options = new ChromeOptions();
options.setProxy(proxy);

Authenticated proxies require extra planning. Embedding credentials in a URL may be rejected by modern browsers and can expose secrets. Prefer these approaches:

  1. IP allowlisting: authorize the fixed public IP of your CI runner or Selenium Grid node so no browser dialog appears.
  2. Local gateway: place a trusted forwarding proxy on the test machine. Selenium connects locally without credentials, and the gateway authenticates upstream.
  3. Controlled browser extension: create an internal Chrome extension that configures the proxy and answers authentication challenges. Load it only from a protected test artifact.
  4. Grid-level configuration: configure networking and secrets on the Selenium node rather than distributing credentials to every test.

Read credentials from a secret manager or environment-specific test configuration. Never commit them to Git, print them to console output, or include them in screenshots. After launch, verify the exit IP with an approved test endpoint before continuing the suite.

Common failure: ERR_PROXY_CONNECTION_FAILED normally indicates the host, port, protocol, or network path is wrong. Repeated 407 Proxy Authentication Required responses indicate missing or rejected credentials.

Using the AutoIt tool

AutoIt can automate Windows dialogs by locating a window, typing credentials, and pressing a button. It was once a common Selenium workaround because WebDriver could not operate browser-owned authentication windows.

The method is fragile: it is Windows-only, requires an unlocked interactive desktop, depends on window titles and timing, and usually fails in headless containers or remote CI agents. It can also reveal passwords through scripts or process arguments.

If a legacy environment leaves no alternative, compile the AutoIt script, store it in a controlled test-artifact location, retrieve credentials securely at runtime, and wait for the exact dialog rather than using a fixed sleep. Restrict this technique to an isolated Windows runner. For maintainable suites, IP allowlisting, a gateway, or centrally configured Grid nodes are safer choices.

Using Alerts

Selenium can handle JavaScript alerts created by webpage code:

Alert alert = new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(ExpectedConditions.alertIsPresent());
alert.accept();

However, a proxy authentication dialog is normally browser chrome, not a JavaScript alert. Therefore, driver.switchTo().alert() often throws NoAlertPresentException even when a credential prompt is visible. The same limitation applies to many HTTP basic-auth dialogs.

Use the alert API only after confirming that WebDriver recognizes the prompt. If it does not, remove the prompt through network configuration or use one of the authenticated-proxy approaches described above. This is more stable than coordinating keystrokes with a window that Selenium cannot inspect.

FAQs

Can Selenium set a proxy directly?

Yes. Selenium 4 can pass HTTP, SSL, or SOCKS settings through the browser's options and proxy capability. This configures the endpoint but may not complete a browser-owned authentication prompt.

Why does username:password@host:port fail in Chrome?

Modern browsers may block credentials embedded in URLs, and the syntax can leak secrets into logs and history. Use IP allowlisting, a secure gateway, or controlled test infrastructure instead.

Can Selenium handle a proxy login with switchTo().alert()?

Usually not. Proxy authentication is commonly browser UI rather than a JavaScript alert, so WebDriver may report that no alert exists.

Does proxy authentication work in headless Chrome?

It can work when authentication is resolved without an interactive prompt. IP allowlisting, a local gateway, or node-level configuration are better suited to headless execution.

Should I use AutoIt for Selenium proxy authentication?

Use it only for a constrained legacy Windows workflow. It is brittle in CI and less secure than eliminating the dialog through network configuration.

Which proxy type is best for browser automation?

It depends on the authorized task. HTTP proxies are straightforward for web traffic, SOCKS5 is flexible, and residential networks can help with location testing. Compare the options in our proxy types guide.