How to set Proxy Authentication in seleniumWebdriver for Chrome Browser

Viewed 33824

I'm trying to Automate a web application selenium 2.0 [webdriver+java].The web application is currently deployed in our UAT servers on our local network.My test cases are executing, but I have to manually enter the Proxy Authentication details for my Chrome instance at the start of the test execution. I have tried all the solutions provided on stack overflow but still, the authentication message pops out.

enter image description here

This is the code I'm using in my driver initializing process

package com.misyn.ess.ui;

import java.util.Arrays;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 *
 * @author User
 */
public class DriverClass {

    private String baseUrl;
    private String driverPath;
    private String driverName;
    private static WebDriver driver;
    private static DriverClass driverClass;

        private DriverClass() {
            try {
                baseUrl = "http://192.168.0.10:8282/ess";
                driverPath = "E:\\Work_Folder\\SelTools\\chromedriver.exe";
                driverName = "webdriver.chrome.driver";

                //Set the location of the ChromeDriver
            System.setProperty(driverName, driverPath);
            //Create a new desired capability
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            // Create a new proxy object and set the proxy
            Proxy proxy = new Proxy();
            proxy.setHttpProxy("192.168.0.200:3128");
            proxy.setSocksUsername("avishka");
            proxy.setSocksPassword("12345678");
            //Add the proxy to our capabilities 
            capabilities.setCapability("proxy", proxy);
            //Start a new ChromeDriver using the capabilities object we created and added the proxy to
            driver = new ChromeDriver(capabilities);

            //Navigation to a url and a look at the traffic logged in fiddler
            driver.navigate().to(baseUrl);


    //            System.setProperty(driverName, driverPath);
    //            driver = new ChromeDriver();
    //            driver.get(baseUrl);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Can anyone give me a solution how to give this proxy username and password thing from the application itself than manually entering details on the pop-up(Authentication), any help would be much appreciated.Thanks

the currently answered one is only for

As of Selenium 3.4 it is still in beta Right now implementation is only done for InternetExplorerDriver

Where I'm using selenium 3.0 and Google Chrome as my web browser.

6 Answers

This code (from Avishka Perera's answer) does not work for me:

    proxy.setSocksUsername("avishka");
    proxy.setSocksPassword("12345678");

The username and password set in this way do not take effect for the http/https proxy - the Proxy Authentication box still popped up.

I'm using Selenium java 3.141.0, ChromeDriver 2.33 and chrome 70. What works for me is to follow Mike's answer here Selenium using Python: enter/provide http proxy password for firefox . Create the zip file, then add the extension like this:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("src/test/resources/proxy.zip"));
WebDriver driver = new ChromeDriver(chromeOptions);

One catch is that the above code will run into error if you set "--headless" argument because chrome in headless mode cannot have extension (Is it possible to run Google Chrome in headless mode with extensions?). If your Chrome runs in Docker container and cannot show the UI, then to get this solution work, you'll need to run with Xvfb instead of in headless mode.

Simple method to add authenticated proxy using selenium wire in Both firefox and chrome

In python

Step:1

pip3 install selenium-wire

Step:2

from seleniumwire import webdriver
from selenium import webdriver

step:3

Add proxy in below-mensioned format

proxy= "username:password@ip:port"
        options = {'proxy': {'http': proxy, 'https': proxy, 'no_proxy': 'localhost,127.0.0.1,dev_server:8080'}}

step:4 pass proxy as an argument

CHROME

driver = webdriver.Chrome(options=chrome_options, executable_path="path of chrome driver", seleniumwire_options=options)

Firefox

driver = webdriver.Firefox(seleniumwire_options=options, executable_path="path of firefox driver", options=firefox_options)

step:5 Verify proxy applied by requesting the url https://whatismyipaddress.com/

time.sleep(20)
driver.get("https://whatismyipaddress.com/")

Note: But selenium log shows it runs in without proxy because we are using an external package to apply proxy.

The approach that worked perfectly fine for me is by using AutoIT.

Install autoIT and prepare a simple script as shown in the picture attached and execute the script file from your testscript using Runtime.getRuntime().exec("\YOUR_SCRIPT.exe") before navigating to the baseURL.

I know this is an old thread, still leaving a solution which worked for me using browsermob proxy, for someone who still needs an option.

Maven dependency:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>net.lightbody.bmp</groupId>
    <artifactId>browsermob-core</artifactId>
    <version>2.1.5</version>
</dependency>

Java Code:

// I am using firefox
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");

BrowserMobProxy browsermobProxy = new BrowserMobProxyServer();
browsermobProxy.setChainedProxy(new InetSocketAddress(PROXY_HOSTNAME, PROXY_PORT));
browsermobProxy.chainedProxyAuthorization(PROXY_USERNAME, PROXY_PASSWORD, AuthType.BASIC);
browsermobProxy.start(0);

FirefoxBinary firefoxBinary = new FirefoxBinary();
FirefoxOptions firefoxOptions = new FirefoxOptions();

firefoxOptions.setBinary(firefoxBinary );
firefoxOptions.setProfile(firefoxProfile);

firefoxOptions.setProxy(ClientUtil.createSeleniumProxy(browsermobProxy));

WebDriver webDriverWithProxy = new FirefoxDriver(firefoxOptionsWithProxy);

webDriverWithProxy.get("https://stackoverflow.com/");
Related