27
Mar

Launching chrome driver with proxy settings in Selenium WebDriver java

Following code snippet demonstrates how to launch chrome driver with proxy server:

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;

public class ChromeAutomationExtension {

public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “C:\\selenium\\chromedriver.exe”);
Proxy proxy = new Proxy();
proxy.setHttpProxy(“rdc-proxy.server.com:8080”);
proxy.setSslProxy(“rdc-proxy.server.com:8080”);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(“proxy”, proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments(“start-maximized”);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get(“http://www.google.com”);
driver.manage().window().maximize();
driver.quit();
}

}

proxy settings summary:

proxy.setProxyType(proxyType) ==> Indicates the type of proxy configuration.
values are: “pac”, “direct”, “autodetect”, “system”, or “manual”.

proxy.setProxyAutoconfigUrl(proxyAutoconfigUrl) ==> Defines the URL for a proxy auto-config file if proxyType is equal to “pac”.
values are: any valid URL

proxy.setFtpProxy(“rdc-proxy.server.com:8080”) ==> Defines the proxy host for FTP traffic when the proxyType is “manual”.
values are: A host and optional port for scheme “ftp”.

proxy.setHttpProxy(“rdc-proxy.server.com:8080”) ==> Defines the proxy host for HTTP traffic when the proxyType is “manual”.
values are: A host and optional port for scheme “http”.

proxy.setNoProxy(noProxy) ==> Lists the address for which the proxy should be bypassed when the proxyType is “manual”.
values are: A List containing any number of Strings.

proxy.setSslProxy(“rdc-proxy.server.com:8080”) ==> Defines the proxy host for encrypted TLS traffic when the proxyType is “manual”.
values are: A host and optional port for scheme “https”.

proxy.setSocksProxy(“rdc-proxy.server.com:8080”) ==> Defines the proxy host for a SOCKS proxy when the proxyType is “manual”.
values are: A host and optional port with an undefined scheme.

proxy.setSocksPassword(password)
proxy.setSocksUsername(username)

A proxyType of “direct” indicates that the browser should not use a proxy at all.

A proxyType of “system” indicates that the browser should use the various proxies configured for the underlying Operating System.

A proxyType of “autodetect” indicates that the proxy to use should be detected in an implementation-specific way.