`
passer_by
  • 浏览: 33182 次
社区版块
存档分类
最新评论

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>



3. webdriver中firefox以代理方式启动
普通情况下,firefox的代理修改是直接修改其配置文件,即prefs.js,把对应的配置修改掉
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); 


firefoxdriver初始化时,我们可以通过配置FirefoxProfile,来修改上面的配置,特别要注意的是localhost的配置,请看下述例子:
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");
		
// 以代理方式启动firefox
FirefoxDriver ff  = new FirefoxDriver(profile);
ff.get("www.xxx.com");
ff.quit();


4. webdriver中IE以代理方式启动,chrome类似
方式不同于firefox,这里是利用webdriver提供的Proxy和WindowsProxyManager来处理,这里也要特别注意localhost的处理。


处理步骤为:
1. InternetExplorerDriver初始化时,调用WindowsProxyManager的backupRegistrySettings方法保存老的代理配置
2. 调用WindowsProxyManager的changeRegistrySettings方法类修改代理配置
3. 程序运行结束后,调用WindowsProxyManager的restoreRegistrySettings来恢复到老的配置。
特别需要提醒的是第3条,我认为webdriver这里处理的不好。恢复到默认配置是在程序结束后,如果程序启动了多个InternetExplorerDriver,每个InternetExplorerDriver保存的是该InternetExplorerDriver启动时IE的配置,而程序结束是调用的shutdownhooker,同时恢复,线程的运行快慢不确定,最后是否恢复到初始配置还很难说,所以,如果同个程序只启动一个InternetExplorerDriver,使用webdriver自带的初始InternetExplorerDriver时修改代理是没有问题,如果启动多个,就要采取其他方式,可以参看本文的第5部分。

程序只启动一个InternetExplorerDriver,以代理模式启动的代码如下:
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();


InternetExplorerDriver初始化后,IE对应的代理配置为:


打开上图中的代理配置文件,可以看到下述配置,具体的可以自己查阅资料:
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';
}


程序结束后,恢复原来配置


5. ie和chrome,手动存储老的代理、修改代理、恢复代理
这里同样使用的是webdriver提供的Proxy和WindowsProxyManager,只不过是我们显示调用而已,不让webdriver帮我们处理。几个接口,我在上面也提起过,这里就直接上代码了。
final WindowsProxyManager proxyManager = new WindowsProxyManager(true,
		"webdriver-ie", 0, 0);
// 备份老代理配置
proxyManager.backupRegistrySettings();

// 增加hooker,jvm退出时,把代理修改为之前的。当然,这里可以自己决定什么时候恢复,比如,在每次InternetExplorerDriver关闭后掉用
new Thread() {
	@Override
	public void run() {
		proxyManager.restoreRegistrySettings(true);
	}
};

// 修改代理
DesiredCapabilities cap = changeProxy("localhost",8080);
proxyManager.changeRegistrySettings(cap);

// 启动ie
WebDriver driver1 = new InternetExplorerDriver(cap);
driver1.get("www.baidu.com");
driver1.close();

// 再次修改代理
DesiredCapabilities cap2 = changeProxy("localhost",80);
proxyManager.changeRegistrySettings(cap);

// 再次启动ie
WebDriver driver2 = new InternetExplorerDriver(cap);
driver2.get("www.google.com");
driver2.close();







  • 大小: 37.8 KB
  • 大小: 29.3 KB
分享到:
评论

相关推荐

    selenium webdriver基于python源码案例.pdf

    Selenium 2默认支持Firefox浏览器,对于其他浏览器如Chrome和IE则需要下载相应的驱动并添加到环境变量中。 - **Selenium 3**:发布于2016年10月左右,对Firefox的支持进行了调整,最高支持版本为46及以下版本。对于...

    selenium webdriver基于python源码案例

    Selenium 的第二个阶段是 Selenium2,Selenium2 合并了 WebDriver,也就是我们通常说的 Selenium,Selenium2 是默认支持 Firefox 浏览器的,这点非常方便。当然也支持其他更多浏览器,IE 和 Chrome 浏览器需要下载...

    selenium设置proxy、headers的方法(phantomjs、Chrome、Firefox)

    ### Selenium 设置 Proxy 和 Headers 方法 (PhantomJS、Chrome、Firefox) 在进行自动化测试或爬虫开发时,Selenium 是一个非常强大的工具。它允许我们控制浏览器执行一系列操作,包括但不限于打开网页、填写表单...

    selenium配置代理.7z

    from selenium.webdriver import Firefox options = FirefoxOptions() options.add_argument('--proxy-server=%s' % '192.168.1.1:8080') # 添加代理选项 profile = FirefoxProfile() profile.set_preference...

    Selenium工具V1.1edit by xbc.pdf

    - 支持多浏览器测试:Firefox、Chrome、IE等主流浏览器。 - 多平台支持:适用于Windows、Linux、Mac OS等多种操作系统。 - 测试与浏览器的兼容性:确保应用程序能够适应不同浏览器和操作系统。 - 测试系统功能:创建...

    how_to_use_http_proxy.pdf

    以火狐浏览器和IE/360浏览器为例,设置代理的步骤分别是: 火狐浏览器: 1. 点击菜单 -&gt; 选项 -&gt; 高级 -&gt; 网络 -&gt; 设置。 2. 在设置窗口中输入代理服务器的IP地址和端口号。 3. 勾选"为所有协议使用相同代理"选项。...

    python+selenium自动化测试

    - **Selenium 2.0 (WebDriver)**:此版本整合了 WebDriver,简化了测试脚本的编写和执行过程,默认支持 Firefox 浏览器。对于 IE 和 Chrome 等其他浏览器,则需要下载对应的驱动并配置环境变量。 - **Selenium 3.0**...

    基于selenium模拟天眼查登录并爬取企业工商信息的python爬虫

    此资源仅供学习用途,当前selenium都是基于无头模式的firefox或者chrome等浏览器进行爬虫抓取,天眼查的反爬技术算是很不错的,仅仅用于个人学习用,并不可以进行大数据的爬取 技术: python selenium 爬虫 模拟登陆...

    selenium的jar包

    它可以支持多种浏览器,包括Chrome、Firefox、IE等,并且兼容多种编程语言,如Java、Python、C#等。Selenium的核心组件包括WebDriver、RC(Remote Control)和IDE(Integrated Development Environment)。 2. **...

    selenium-2.38.3.tar.gz

    每个特定的浏览器(如Chrome、Firefox)都有对应的WebDriver实现。 2. WebElement:WebDriver与页面元素进行交互的桥梁。通过这个接口,我们可以定位到页面上的元素,并执行点击、输入等操作。 3. Browsermob ...

    python 爬虫学习测试样例 包含逆向js,Selenium框架等

    Selenium支持多种浏览器(如Chrome、Firefox),可以模拟用户行为,如点击按钮、填写表单、滚动页面等。通过Selenium,我们可以直接控制浏览器执行JavaScript,获取原本无法直接抓取的数据。使用Selenium的关键在于...

    知网_python_

    在Python中,我们通常使用`selenium.webdriver`模块来启动浏览器,比如Chrome或Firefox。例如: ```python from selenium import webdriver # 初始化Chrome浏览器 driver = webdriver.Chrome() ``` 接着,我们...

    html2canvas

    The other set of tests run Firefox, Chrome and Internet Explorer with webdriver. The selenium standalone server (runs on Java) is required for these tests and can be downloaded from here. They capture...

    python+selenium实现自动化百度搜索关键词

    - Selenium是一个强大的自动化测试工具,支持多种浏览器,如Chrome、Firefox等。 3. **ChromeDriver安装与配置** - ChromeDriver是Selenium用于控制Chrome浏览器的驱动程序。 - 访问ChromeDriver官方下载页面 ...

Global site tag (gtag.js) - Google Analytics