
📌How to Handle Proxy Authentication in Selenium WebDriver
🛡️ What is a Proxy?
Handle Proxy Authentication: A proxy server acts as a gateway between a client and the internet. It helps:
- Improve privacy
- Provide security
- Enable content filtering (e.g., in schools or companies)
In web automation, proxies are often used when your application needs to run behind secure networks or restricted environments. Sometimes, these proxies require username and password authentication, which leads to a popup window — something that Selenium alone cannot handle.
📌 Difference Between SOCKS and HTTP Proxy
Feature | SOCKS Proxy | HTTP Proxy |
Full Form | Secure Sockets | Hypertext Transfer Protocol Proxy |
Layer | Transport Layer | Application Layer |
Use Case | All protocols (TCP/UDP) | Only HTTP/HTTPS |
Data Handling | Doesn’t interpret traffic | Interprets HTTP data |
Performance | Slightly slower | Better performance |
Best For | Complex traffic filtering | Web automation and scraping |
📌 How to Handle Proxy Authentication in Selenium (Chrome Browser)
Handle Proxy Authentication: Selenium cannot handle OS-level authentication dialogs (like HTTP proxy popups) directly. There are three ways to bypass this limitation:
- Passing credentials in the URL (e.g., http://username:password@website.com)
- Using AutoIT
- Using Alert Popups in Selenium
We will focus on the second and third options, which are more reliable.
📌 Method 1: Using AutoIT to Handle Proxy Popups
Why AutoIT?
Handle Proxy Authentication: Selenium WebDriver can only automate web-based popups, not OS-level popups. AutoIT is a Windows-based automation tool designed to handle these limitations.
📌 Steps to Handle Proxy Using AutoIT
- Install AutoIT on your Windows machine
- Open SciTE Script Editor
- Paste the script below and save it as ProxyAuthentication.au3
- Compile the file into an .exe using the AutoIT compiler
AutoIT Script:
autoit
WinWaitActive(“Authentication Required”)
Send(“your-username”)
Send(“{TAB}”)
Send(“your-password”)
Send(“{ENTER}”)
📌 Selenium Integration Code (Java):
java
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandleProxyWithAutoIT {
public static void main(String[] args) throws IOException {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
// Trigger AutoIT script
Runtime.getRuntime().exec(“path/to/ProxyAuthentication.exe”);
WebDriver driver = new ChromeDriver();
driver.get(“http://your-proxy-protected-url.com”);
// Your test logic
driver.quit();
}
}
✅ Output:
AutoIT handles the popup silently. Selenium continues the test once authentication is complete.
📌 Method 2: Using Alerts in Selenium WebDriver
If the proxy authentication appears as a browser alert (rare, but possible), Selenium can handle it using its Alert interface.
Test Scenario:
- Launch the Chrome browser
- Open a URL that triggers a basic authentication popup
- Enter credentials using sendKeys() on the alert popup
Selenium Code Using Alerts:
java
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ProxyAuthUsingAlert {
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
driver.get(“http://your-proxy-protected-url.com”);
// Handle username alert
Alert alert = driver.switchTo().alert();
alert.sendKeys(“your-username”);
alert.accept();
// Handle password alert
Alert alert2 = driver.switchTo().alert();
alert2.sendKeys(“your-password”);
alert2.accept();
// Continue test
driver.quit();
}
}
✅ Output:
Proxy popup is handled directly within Selenium using alerts. Authentication completes successfully.
📌 Summary: Best Way to Handle Proxy Authentication in Selenium
Method | Use Case | Reliability |
URL with credentials | Only works if the browser allows this method | ⚠️ Low |
AutoIT Tool | OS-level popup handling | ✅ High |
Alert Handling | Only works for browser-based alerts | ✅ Medium |
📌 Final Thoughts
- Use AutoIT for handling proxy authentication popups on Windows
- Use Selenium Alert interface if the popup appears as a browser alert
- Avoid embedding credentials in the URL as it’s insecure and not browser-compatible anymore
✅ Pro Tip: Always test your authentication logic in different environments (Dev, QA, Prod) to make sure the proxy behaves consistently.