BrowserMobProxy不会将任何条目捕获到har文件中

时间:2019-09-16 11:21:45

标签: java selenium browsermob-proxy

尝试捕获某些测试页localhost:8000上的http标头,而entry部分中的HAR文件似乎没有任何内容。

我的代码:

            BrowserMobProxyServer proxy = new BrowserMobProxyServer();

            proxy.start(0);
            Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
            seleniumProxy.setSslProxy("trustAllSSLCertificates");
            try {
                String hostIp = Inet4Address.getLocalHost().getHostAddress();
                seleniumProxy.setHttpProxy(hostIp + ":" + proxy.getPort());
                seleniumProxy.setSslProxy(hostIp + ":" + proxy.getPort());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            WebDriverManager.chromedriver().setup();
            DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();
            seleniumCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
            seleniumCapabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);

            ChromeOptions options = new ChromeOptions();
            options.addArguments("--ignore-certificate-errors");
            options.addArguments("--user-data-dir=temp");
            options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            options.merge(seleniumCapabilities);
            webDriver = new ChromeDriver();
            proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

            proxy.setHarCaptureTypes(CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_HEADERS);
            proxy.enableHarCaptureTypes(CaptureType.REQUEST_HEADERS,CaptureType.RESPONSE_HEADERS);
            proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
            driver.get("http://localhost:8000")
            proxy.newHar("Test");

http://localhost:8000打开时,我可以在console>networks标签中看到8个项目 但是har文件是:

{"log":{"version":"1.2","creator":{"name":"BrowserMob Proxy","version":"2.1.4","comment":""},"pages":[{"id":"Test","startedDateTime":"2019-09-16T11:06:02.020Z","title":"Test","pageTimings":{"comment":""},"comment":""}],"entries":[],"comment":""}}

翻阅不计其数的页面,似乎所有其他人都只有http和无头的问题。

1 个答案:

答案 0 :(得分:0)

找到了解决方案。 更改字符串hostIp =“ localhost”确实解决了该问题。