使用BrowserMobProxy,Selenium,Firefox,marionette / gecko获取请求和响应 [英] Getting Request And Response Using BrowserMobProxy, Selenium, Firefox, marionette/gecko
问题描述
我正在尝试使用BMP的RequestFilter和ResponseFilter获取响应和请求。但是,当网页加载时,控制台中没有任何内容被打印出来。
其他一切似乎都有效。也许BMP没有看GeckoDriver?
我使用的是Firefox 50.0,BrowserMobProxy 2.1.2,Selenium 3.0.1和GeckoDriver 0.11.1
测试代码如下。有人可以帮帮我吗?
非常感谢你!
I'm trying to get response and request using BMP's RequestFilter and ResponseFilter. However, when the webpage loads, nothing gets printed in the console.
Everything else seems to work though. Maybe BMP is not watching GeckoDriver?
I'm using Firefox 50.0, BrowserMobProxy 2.1.2, Selenium 3.0.1, and GeckoDriver 0.11.1
The testing code is below. Could someone please help me?
Thank you very much!
BrowserMobProxy server = new BrowserMobProxyServer();
server.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
server.start();
int port = server.getPort();
server.addRequestFilter((request, content, info) -> {
String q = URLDecoder.decode(info.getOriginalUrl(), "UTF-8");
System.out.println("Request: "+q);
return null;
});
server.addResponseFilter((response, content, info) -> {
String type = response.headers().get("Content-Type");
System.out.println("Response: "+info.getOriginalRequest());
System.out.println(type);
});
Proxy proxy = ClientUtil.createSeleniumProxy(server);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);
capabilities.setCapability("marionette", true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
FirefoxProfile fp = new FirefoxProfile();
capabilities.setCapability(FirefoxDriver.PROFILE, fp);
String gecko = "d:/Programming/java/geckodriver.exe";
System.setProperty("webdriver.gecko.driver", gecko);
driver = new FirefoxDriver(capabilities);
driver.get("https://google.com");;
推荐答案
在Firefox 51及更低版本中,有一个错误/ Selenium 3的GeckoDriver缺少功能,在 DesiredCapabilities
对象上设置 CapabilityType.PROXY
时阻止Firefox获取代理设置。
In Firefox 51 and lower, there is a bug/missing feature in Selenium 3's GeckoDriver that prevents Firefox from picking up the proxy settings when setting CapabilityType.PROXY
on the DesiredCapabilities
object.
但是,您仍然可以直接在 FirefoxProfile
上设置代理设置。在 BMP测试之一。由于您已经使用了 FirefoxProfile
对象,这可能是一个明智的解决方案。它看起来像这样(用适当的主机名/ IP地址替换localhost):
However, you can still set the proxy settings directly on the FirefoxProfile
. There's an example of this in one of BMP's tests. Since you're already using a FirefoxProfile
object, this would probably be a sensible solution for you. It would look something like this (replace localhost with a hostname/ip address as appropriate):
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("network.proxy.http", "localhost");
fp.setPreference("network.proxy.http_port", server.getPort());
fp.setPreference("network.proxy.ssl", "localhost");
fp.setPreference("network.proxy.ssl_port", server.getPort());
fp.setPreference("network.proxy.type", 1);
fp.setPreference("network.proxy.no_proxies_on", "");
此geckodriver问题还讨论了在 DesiredCapabilities $ c上使用
CapabilityType.PROXY
的一些其他替代方法$ c>对象。
This geckodriver issue also discusses a few other alternatives to using CapabilityType.PROXY
on the DesiredCapabilities
object.
更新
根据 mozilla bug报告,这个问题在Firefox 52中修复,计划于2017年3月7日发布。与此同时,解决方案为 FirefoxProfile
应该使用51(及更低版本),并且还应继续使用52 +。
According to the mozilla bug report, this issue is fixed in Firefox 52, which is scheduled to be released on March 7, 2017. In the meantime, the solution with the FirefoxProfile
should work with 51 (and lower), and should also continue to work with 52+.
这篇关于使用BrowserMobProxy,Selenium,Firefox,marionette / gecko获取请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!