Search code examples
proxyselenium-chromedriverbasic-authenticationheadlesschrome-options

Chrome headless browser with corporate proxy authetication not working


I am trying to run a Chrome headless browser sitting behind a corporate proxy. I tried below code. But unable to pass through it.

public class HeadlessChrome 
{
    WebDriver driver;

    @Test
    public void createChromeDriverHeadless() throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver", "D:\\LocalData\\workspace\\Drivers and Libraries\\driver\\chromedriver.exe");

        ChromeOptions chromeOptions = new ChromeOptions();

        Proxy proxy = new Proxy();
        proxy.setHttpProxy("http://user:pwd@server:port");
        proxy.setSslProxy("http://user:pwd@server:port");

//      chromeOptions.setCapability("proxy", proxy);
        chromeOptions.addArguments("--proxy-server=user:pwd@server:port");
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--disable-gpu");
        chromeOptions.addArguments("start-maximized");  

        driver = new ChromeDriver(chromeOptions);
        driver.get("http://seleniumhq.org");       

        Thread.sleep(5000);
        System.out.println("Title : " + driver.getTitle());
        assertTrue(driver.findElement(By.id("q")).isDisplayed());
        driver.quit();
    }
}

Please help me out.


Solution

  • If you were not using headless you could have used the approach in below link

    user:pass proxies with selenium

    But with headless extension are currently not allowed. So now your option is add another proxy

    chrome -> (intermediate proxy w/o auth) -> corporate proxy w/ auth -> internet
    

    One options is to use polipo

    https://www.irif.fr/~jch/software/polipo/

    with below config

    parentAuthCredentials=username:password
    parentProxy=corporateproxy:port
    

    and then use

    chromeOptions.addArguments("--proxy-server=http://polipoproxy:port");
    

    The default would be 127.0.0.1:8123 in don't override in polipo config.

    Other options you can use

    • Use squid proxy instead of polipo

    • Write your own proxy forwarder using python or node or any other language you are comfortable with