1

I have setup proxy for PhantomJS and verified the IP address using below code. It works fine.

Here the Host name is IP address: 43.19.196.36

DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setJavascriptEnabled(true);
System.setProperty("phantomjs.binary.path", "../Drivers/phantomjs.exe");

ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=hostname:port");
cliArgsCap.add("--proxy-auth=username:password");
cliArgsCap.add("--proxy-type=http");
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);

driver = new PhantomJSDriver(capabilities);

driver.get("http://api.ipify.org/");
logger.info("IP Address: " + Jsoup.parse(driver.getPageSource()).body().text());

This code works fine and my application is using proxy address.

2017-07-24 16:37:47 INFO  ElectoralRoll:81 - IP Address: 42.10.197.10

But, How to setup the same proxy for chrome? Here is my code. It prints my IP address. It is not using the proxy.

Proxy proxy = new Proxy();

proxy.setHttpProxy("hostname:port");
proxy.setFtpProxy("hostname:port");
proxy.setSslProxy("hostname:port");
proxy.setSocksUsername("username");
proxy.setSocksPassword("password");

capabilities.setCapability(CapabilityType.PROXY, proxy);

This prints my IP address:

2017-07-24 16:37:47 INFO  ElectoralRoll:81 - IP Address: 

I have also tried this. But did not work.

capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://username:password@hostname:port"));

Referred: https://stackoverflow.com/a/19225780/8329042

Edit: I have added: (@Riaz's answer)

proxy.setProxyType(ProxyType.MANUAL);

Using this in my code, it says:

2017-07-26 20:22:03 INFO  ElectoralRoll:83 - IP Address: proxy authorization required
Abhilash
  • 435
  • 5
  • 15

2 Answers2

2

Try reviewing the code here. Particularly these bits:

proxy.setProxyType(ProxyType.MANUAL);

capability.setCapability(CapabilityType.PROXY, proxy);

I'm not aware of any special needs in Chrome for this to work, so it's probably something basic.

Riaz
  • 874
  • 6
  • 15
2

Looks like there is an issue using Basic authentication with Chrome.

So I have added my IP address to the "Authenticated IP or Hostname" in Proxymesh Dashboard. With this I have bypassed the username and password authentication.

Proxy proxy = new Proxy();
proxy.setHttpProxy("hostname:port");
proxy.setFtpProxy("hostname:port");
proxy.setSslProxy("hostname:port");
// proxy.setSocksUsername("username");
// proxy.setSocksPassword("password");
proxy.setAutodetect(false);
proxy.setProxyType(ProxyType.MANUAL);
capabilities.setCapability(CapabilityType.PROXY, proxy);

With this I am able to use proxy using chrome. This solution is specific to ProxyMesh provider.

kohane15
  • 809
  • 12
  • 16
Abhilash
  • 435
  • 5
  • 15