How to Handle Proxy Authentication in Selenium Webdriver

What is a Proxy?

A proxy acts as an intermediary between clients sending requests and server responding. The primary use of a proxy is to maintain privacy and encapsulation between multiple interactive systems.

A proxy can also add another layer of security on the web by acting as a firewall between Client and web servers. This is especially used when the websites that clients use have to be labeled as allowed or blocked based on the website content.

This process is known as ‘Content Filtering’ and is most commonly used in Educational Institutions, Corporate Offices, etc. Content Filtering can be easily accomplished with the help of a proxy. In terms on the Internet, a proxy can be implemented as a separate server which stands in between client machines and actual web or database servers responding.

Difference between SOCKS and HTTP Proxy

SOCKS HTTP Proxy
SOCKS stands for secured sockets. It is generally used as a firewall between the Client and the server HTTP Proxy can also be used as a firewall between the Client and the server but can be used only for HTTP Requests
SOCKS does not interpret the data being exchanged HTTP Proxy interprets the data being exchanged between the Client and the server
Slower in terms of performance Better performance compared to SOCKS

How to Handle Proxy in Selenium Webdriver in Chrome

HTTP Proxy authentication with Selenium in Chrome can be handled using the following approaches

  • Using the AutoIT tool
  • Using Alerts

Using the AutoIT tool

Auto IT is a third party tool that is used for windows desktop automation. Since Selenium only handles web-based popups and windows, handling operating system controls is not possible using Selenium.

This requires the use of external third-party tools such as Auto IT to be integrated with Selenium. For that, Auto IT is most commonly used for handling file uploads and file downloads on websites.

To use AutoIT, You need to download and install AutoIT software on our local machines.

Download an install of AutoIT is covered in the article.

Open Programs – Autoit tool – SciTE Script Editor and add the below mentioned AutoIt script in Autoit editor and save it as ‘ProxyAuthentication.au3’ in your system

Compile the file and Convert it as ‘ProxyAuthentication.exe.’

In Eclipse, add the Auto IT file to Selenium Script and run

Below is the AutoIT script for HTTP Proxy authentication

Send("guru99{ENTER}")
Send("guru99{ENTER}")

You need to pass the Auto IT file for execution on Selenium webdriver using the below code

Source Code:

package Guru99Demo;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AutoITDemo {
    public static void main(String[] args) throws IOException {
        System.setProperty("webdriver.chrome.driver", "D:\\ chromedriver.exe");;
        WebDriver driver = new ChromeDriver();
        driver.get("http://demo.guru99.com/test/basic_auth.php");
        //Passing the AutoIt Script to Selenium	
        Runtime.getRuntime().exec("D:\\Data_Personal\\ProxyAuthentication.exe");
    }
}

Code Explanation:

  • In the first step, You are initializing an instance of Chrome driver by setting the system property to point the chromedriver.exe file
  • In the second step, You are then initializing an object of web driver and passing the website URL using get method
  • Finally, You are passing username and password to HTTP Proxy authentication popup using an AutoIT script file

Code Output:

Username Alert Handling Using AutoIT
Username Alert Handling Using AutoIT
Password Alert Handling Using AutoIT

Password Alert Handling Using AutoIT
Proxy Authentication Success Screen

Proxy Authentication Success Screen

Using Alerts

Alerts are simple, inbuilt feature provided by Selenium web driver. You can handle proxy authentication popups using Selenium web driver by switching to the HTTP proxy authentication alert and passing the user name and password directly to the alert. With the help of send keys method.

Example:

Test Scenario:

Handle the HTTP Proxy authentication popup on the website: https://demo.guru99.com/test/basic_auth.php using Alerts in Selenium web driver.

Source code Example:

package Guru99Demo;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AlertsDemo {
    public static void main(String args[]) throws IOException {
        System.setProperty("webdriver.chrome.driver", "D:\\Data_Personal\\Software\\chromedriver_win32\\chromedriver.exe");;
        WebDriver driver = new ChromeDriver();
        driver.get("http://demo.guru99.com/test/basic_auth.php");
        // Handling Username alert
        driver.switchTo().alert().sendKeys("guru99");
        driver.switchTo().alert().accept();
        // Handling Password alert
        driver.switchTo().alert().sendKeys("guru99");
        driver.switchTo().alert().accept();
    }
}

Code Output

Proxy Authentication done successfully.

Code Explanation:

  • Initially, You are instantiating an instance of Chrome driver by setting the webdriver property to point the location of chromedriver.exe file
  • Then, You are opening the URL of required website by passing the URL as a parameter to driver.get() method
  • Once URL is opened, You are switching to the username alert and sending the username – guru99. You are then clicking on OK button on the alert using alert.accept method
  • Then, You are switching to the password alert and sending the password – guru99. You are then clicking on OK button on the alert using alert.accept method

Summary

  • A proxy acts as an intermediary between clients sending requests and server responding. The basic use of a proxy is to maintain privacy and encapsulation between multiple interactive systems.
  • HTTP Proxy authentication with Selenium in Chrome can be handled using the following approaches
  • Passing username and password in the website URL
  • Using the AutoIT tool
  • Using Alerts
  • Of the above three approaches, using alerts is the most effective way to handle HTTP Proxy authentication in Selenium webdriver