Steps to handle Proxy Authentication using Selenium Webdriver

Prerequisites:

Example 1: Manual Proxy Authentication Handling

We have cretated a PHP file (selenium_auth.php) that accepts a proxy authentication request of user name and password:

PHP




// Program for Manual Autentication
 
<?php
$php_variable = "Hello from w3wiki!";
echo '<html>
 
<head>   
    <title>Selenium Authentication</title>
         
</head>
<body>
  <head>
 
</head>
    
<div style="margin-left:20px;">
    <script>
        var username = prompt("Enter username-", "");
        var password = prompt("Enter Password", "");
         
        if (username == "gfg" && password == "gfg") {
            document.write("Proxy Authentication Passed."); 
        }
         
        if(!username || !password){
            document.write("Not successful");
        }
         
        if (username !== "gfg" && password !== "gfg" && username !== "" && password !== "") {
            alert("Wrong User Name and Passowrd");
        }
    </script>
 
    <?php
    // PHP code
    echo <p>Hello from w3wiki!</p>
</div>
</body>
</html>';
?>


Once we load it into any server such as the localhost, we can see that it is providing the following output:

Output:

As we can see if we are manually handling the proxy authentication once we provide the username and password as β€œgfg” it says β€œproxy authentication passedβ€œ, Now let’s learn how to do that using the selenium webdriver automatically once a proxy authentication request has been prompted on our screen.

Example 2: Selenium Webdriver Proxy Authentication

Step 1: Use any JDK-enabled Java editor and load the Selenium JAR files. We will be using the IntelliJ editor to execute the code.

Note: To handle proxy authentication without third-party apps in Selenium, use built-in alerts. Switch to the HTTP proxy authentication alert and pass credentials using β€œsendKeys” method.

Step 2: After this, we will need to write the Java code that will enter the credentials automatically. We can use the Example 1 authentication file (selenium_auth.php) for demonstration.

Java




// Java program for handling alerts
// using Selenium WebDriver.
 
package gfg;
 
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
    {
        // Setting the system property to the location of
        // the ChromeDriver executable
        System.setProperty(
            "webdriver.chrome.driver",
            "C:\\Users\\kisha\\Downloads\\chromedriver_win32\\chromedriver.exe");
 
        // Creating a new instance of the ChromeDriver
        WebDriver driver = new ChromeDriver();
 
        // Opening a web page using the WebDriver
        driver.get(
            "http://localhost/selenium-auth/selenium_auth.php");
 
        // Handling the username alert:
        // Entering the username "gfg" into the alert and
        // accepting it
        driver.switchTo().alert().sendKeys("gfg");
        driver.switchTo().alert().accept();
 
        // Handling the Password alert:
        // Entering the password "gfg" into the alert and
        // accepting it
        driver.switchTo().alert().sendKeys("gfg");
        driver.switchTo().alert().accept();
    }
}


In this above code, we will only have to make the necessary changes on the driver.get the URL, and the System.set Property so have a look at the URL on your computer and set the path to where you have downloaded the Chromium webdriver.

Step 3: Now when we run will run the Java program in any compiler. This will send two alert keys β€œgfg” and β€œgfgβ€œ, which will be fetched into the browser and the browser will successfully redirect us to the page where we will see the notification that we successfully managed to do the proxy authentication.

Step 4: We can now see in the output below we have successfully used the alert function provided by the Selenium web driver to handle proxy authentication:

Output:

How does Selenium Webdriver Handle Proxy Authentication?

Selenium is an open-source tool that provides us with the libraries that are used for browser automation the selenium browser allows us to test different functionalities in our website, thus the Selenium web driver comes very handy in day life if we are regularly testing web applications or doing automation tasks.

Similar Reads

What is a Proxy?

In general, a proxy is a server application or interface acting as a middleman between a client making a request and the server providing the requested resource. In the context of Selenium, a proxy serves as an intermediary between the user (client) and the server from which data is requested....

What is Proxy Authentication?

Proxy authentication involves the process of gaining access to a specific resource behind a proxy server. The HTTP proxy authenticate response header defines it as a method used to access a resource available behind a proxy. In proxy authentication, requests are made to the proxy server, and upon successful authentication, the proxy server allows the transmission of the request to access the desired resource....

SOCKS vs HTTP Proxy

...

Steps to handle Proxy Authentication using Selenium Webdriver

Prerequisites:...

Conclusion

...