Set Selenium proxy programmatically

时间:2016-04-07 10:36:53

标签: java selenium selenium-webdriver selenium-firefoxdriver

In my automation project I need to set proxy server. I tried with system variable settings and profile setting for firefox browser. But those technique does not work for me. Please any one help me in this regard.

Note: I also tried with executing shell command using java but I got stuck when password is asked.

1 个答案:

答案 0 :(得分:1)

您绝对不需要设置任何系统级属性。这是在Firefox中执行此操作的一种方法:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1); // Manual proxy config
profile.setPreference("network.proxy.http", "proxy3.proxy.net");
profile.setPreference("network.proxy.http_port", 3128);
profile.setPreference("network.proxy.ssl", "proxy3.proxy.net");
profile.setPreference("network.proxy.ssl_port", 3128);

WebDriver driver = new FirefoxDriver(profile);

或者更灵活,更少浏览器的替代方案:

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy("proxy3.proxy.net:3128");
proxy.setSslProxy("proxy3.proxy.net:3128");

DesiredCapabilities caps = DesiredCapabilities.firefox();  // or chrome() etc.
caps.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new FirefoxDriver(caps);