selenium webdriver 以代理proxy方式启动firefox,ie,chrome
原创文章,转载请注明出处:http://passerbyy.iteye.com/blog/1286292 作者:passer_by
1.题前话
没有发现之前,自己傻不垃圾的自己写了各个浏览器修改代理的方法,结果发现webdriver有现成,悔恨不已,希望其他同仁能够少走弯路。
本文是在Webdriver 2.12.0下面测试得到的结论
2. webdriver的maven配置
<repositories><repository><id>selenium</id><name>selenium</name><url>http://repo1.maven.org/maven2/</url></repository></repositories><dependencies><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>2.12.0</version></dependency></dependencies>
user_pref("network.proxy.ftp_port", 1000); user_pref("network.proxy.gopher", "10.0.0.0"); user_pref("network.proxy.gopher_port", 1000); user_pref("network.proxy.http", "10.0.0.0"); user_pref("network.proxy.http_port", 1000); user_pref("network.proxy.no_proxies_on", ""); user_pref("network.proxy.share_proxy_settings", true); user_pref("network.proxy.socks", "10.0.0.0"); user_pref("network.proxy.socks_port", 1000); user_pref("network.proxy.ssl", "10.0.0.0"); user_pref("network.proxy.ssl_port", 1000); user_pref("network.proxy.type", 1);
String proxyIp = "localhost";int proxyPort = 8080;FirefoxProfile profile = new FirefoxProfile();// 使用代理profile.setPreference("network.proxy.type", 1);// http协议代理配置profile.setPreference("network.proxy.http", proxyIp);profile.setPreference("network.proxy.http_port", proxyPort);// 所有协议公用一种代理配置,如果单独配置,这项设置为false,再类似于http的配置profile.setPreference("network.proxy.share_proxy_settings", true);// 对于localhost的不用代理,这里必须要配置,否则无法和webdriver通讯profile.setPreference("network.proxy.no_proxies_on", "localhost");// 以代理方式启动firefoxFirefoxDriver ff = new FirefoxDriver(profile);ff.get("www.xxx.com");ff.quit();
String proxyIpAndPort= "localhost:8080";// 代理配置DesiredCapabilities cap = new DesiredCapabilities();org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();// 配置http、ftp、ssl代理(注:当前版本只支持所有的协议公用http协议,下述代码等同于只配置http)proxy.setHttpProxy(proxyIpAndPort) .setFtpProxy(proxyIpAndPort) .setSslProxy(proxyIpAndPort);// 以下三行是为了避免localhost和selenium driver的也使用代理,务必要加,否则无法与iedriver通讯cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);System.setProperty("http.nonProxyHosts", "localhost");cap.setCapability(CapabilityType.PROXY, proxy);WebDriver driver = new InternetExplorerDriver(cap);driver.get("www.baidu.com");driver.close();
function FindProxyForURL(url, host) { if (shExpMatch(host, 'localhost')) { return 'DIRECT'; } if (shExpMatch(url, '*/selenium-server/*')) { return 'PROXY localhost:0; DIRECT'; } return 'PROXY 10.16.16.38:3229';}
final WindowsProxyManager proxyManager = new WindowsProxyManager(true,"webdriver-ie", 0, 0);// 备份老代理配置proxyManager.backupRegistrySettings();// 增加hooker,jvm退出时,把代理修改为之前的。当然,这里可以自己决定什么时候恢复,比如,在每次InternetExplorerDriver关闭后掉用new Thread() {@Overridepublic void run() {proxyManager.restoreRegistrySettings(true);}};// 修改代理DesiredCapabilities cap = changeProxy("localhost",8080);proxyManager.changeRegistrySettings(cap);// 启动ieWebDriver driver1 = new InternetExplorerDriver(cap);driver1.get("www.baidu.com");driver1.close();// 再次修改代理DesiredCapabilities cap2 = changeProxy("localhost",80);proxyManager.changeRegistrySettings(cap);// 再次启动ieWebDriver driver2 = new InternetExplorerDriver(cap);driver2.get("www.google.com");driver2.close();