How to set proxy authentication in PhantomJS using selenium?

PhantomJS uses the three proxy options that are set from the commandline (docs).

  • --proxy=address:port specifies the proxy server to use (e.g. --proxy=192.168.1.42:8080).
  • --proxy-type=[http|socks5|none] specifies the type of the proxy server (default is http).
  • --proxy-auth specifies the authentication information for the proxy, e.g. --proxy-auth=username:password).

To use these, you have to add them to the DesiredCapabilities map (as seen in this answer):

ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=address:port");
cliArgsCap.add("--proxy-auth=username:password");
cliArgsCap.add("--proxy-type=http");
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability(
    PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
WebDriver driver = new PhantomJSDriver(capabilities);

Leave a Comment