我无法使用browsermob(selenium + PhantomJS浏览器)捕获请求和响应
请参阅示例代码
server = new BrowserMobProxyServer();
server.start(0);
server.newHar("contracts");
功能:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true); capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
new String[] {"--web-security=false", "--ssl-protocol=any",
"--ignore-ssl-errors=yes"});
capabilities.setCapability("takeScreenshot", true);
URL hubUrl = new URL("http://152.188.0.42:5555/wd/hub");
server.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
启动应用程序:
driver = new RemoteWebDriver(hubUrl,capabilities);
driver.get("http://www.google.com");
创建har文件:
Har har = server.getHar();
FileOutputStream fos = new FileOutputStream("runnowNew.har");
har.writeTo(fos);
使用上述代码生成的Har:
{
"log":
{
"version":"1.2",
"creator":{"name":"BrowserMob Proxy","version":"2.1.2","comment":""},
"pages":[{"id":"contracts","startedDateTime":"2016-10-05T12:56:33.460+05:30","title":"contracts","pageTimings":{"comment":""},"comment":""}],
"entries":[],
"comment":""
}
}
答案 0 :(得分:1)
我认为问题在于您不使用BrowserMob Proxy
作为Selenium流量的代理。
您需要将Selenium(ip& port)的代理设置为BrowserMob Proxy
中配置的代理。
由于您在本地工作,因此IP可能为127.0.0.1
,您可以使用server.getPort()
来获取BrowserMob Proxy
正在侦听的端口。