1

我无法使用 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":""
  }
}
4

1 回答 1

1

我认为问题在于您没有BrowserMob Proxy用作 Selenium 流量的代理。您需要将 Selenium 的代理(ip & port)设置为BrowserMob Proxy. IP可能是127.0.0.1因为你在本地工作,你可以用它server.getPort()来获取BrowserMob Proxy正在监听的端口。

于 2016-12-12T08:22:01.267 回答