我已经创建了从Chrome获取网络流量的简单方法:
public void saveNetworkTraffic() {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/bin/chromedriver");
String sFileName = "networklog.xar";
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
WebDriver driver = new ChromeDriver(capabilities);
WebDriverRunner.setWebDriver(driver);
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
proxy.newHar("google.com");
driver.get("http://google.com/");
Har har = proxy.getHar();
File harFile = new File(sFileName);
try {
har.writeTo(harFile);
} catch (IOException ex) {
System.out.println(ex.toString());
System.out.println("Could not find file " + sFileName);
}
}
当浏览器打开页面时,它显示错误" ERR_EMPTY_RESPONSE"
在此步骤driver.get("http://google.com/")
而不是Chrome中的常规Google页面。
我试图弄清楚错误的原因,但根据https://github.com/lightbody/browsermob-proxy#using-with-selenium,我的代码应该可以正常工作。
答案 0 :(得分:0)
您的代码看起来不错,请尝试调整依赖项,例如:
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
</dependency>
答案 1 :(得分:0)
添加此代码,然后尝试:
proxy.addFirstHttpFilterFactory(new ResponseFilterAdapter.FilterSource(new ResponseFilter() {
@Override
public void filterResponse(HttpResponse response, HttpMessageContents contents, HttpMessageInfo messageInfo) {}}, Integer.MAX_VALUE));