我正在尝试使用带有selenium的browsermob/browserup代理从chrome获取*.har日志。在本地计算机上以嵌入式模式与java一起使用:
dependencies{
compile 'net.lightbody.bmp:browsermob-core:2.1.5'
compile 'com.browserup:browserup-proxy-core:2.1.1'
compile 'org.seleniumhq.selenium:selenium-chrome-driver:3.141.59'
}
当我尝试这个:
@SneakyThrows
public static void tryStackOverflow() {
System.setProperty("webdriver.chrome.driver", "C:\\Chrome\\chromedriver\\chromedriver.exe");
BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
browserMobProxy.setTrustAllServers(true);
browserMobProxy.start();
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(browserMobProxy);
String hostIp = Inet4Address.getLocalHost().getHostAddress();
seleniumProxy.setHttpProxy(hostIp + ":" + browserMobProxy.getPort());
seleniumProxy.setSslProxy(hostIp + ":" + browserMobProxy.getPort());
ChromeOptions launchOptions = new ChromeOptions();
launchOptions.setProxy(seleniumProxy);
launchOptions.setBinary("C:\\Chrome\\chrome.exe");
launchOptions.addArguments(List.of("--ignore-certificate-errors"));
ChromeDriver chrome = new ChromeDriver(launchOptions);
browserMobProxy.newHar(); // creating new HAR
chrome.get("https://www.stackoverflow.com/");
List<HarEntry> entries = browserMobProxy.getHar().getLog().getEntries();
for (HarEntry entry : entries) {
System.out.println(entry.getRequest().getUrl());
}
try(OutputStream outputStream = Files.newOutputStream(Path.of(
"C:\\stack.test.file.har"), StandardOpenOption.WRITE, StandardOpenOption.CREATE)
) {
browserMobProxy.getHar().writeTo(outputStream);
} catch (Exception ex) {
log.info("Save error = ", ex);
}
browserMobProxy.stop();
chrome.close();
}
一切正常:创建了新的chrome示例,浏览器导航到网站,然后“stack.test.file.har”文件充满了数据(harentries)。我不需要启动新的chrome示例,但需要连接到已有的chrome示例。执行此操作时,会得到空的har日志文件:
@SneakyThrows
public static void tryGitHub() {
System.setProperty("webdriver.chrome.driver", "C:\\Chrome\\chromedriver\\chromedriver.exe");
BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
browserMobProxy.setTrustAllServers(true);
browserMobProxy.start();
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(browserMobProxy);
String hostIp = Inet4Address.getLocalHost().getHostAddress();
seleniumProxy.setHttpProxy(hostIp + ":" + browserMobProxy.getPort());
seleniumProxy.setSslProxy(hostIp + ":" + browserMobProxy.getPort());
ChromeOptions connectOptions = new ChromeOptions();
connectOptions.setProxy(seleniumProxy);
connectOptions.setBinary("C:\\Chrome\\chrome.exe");
connectOptions.addArguments(List.of("--ignore-certificate-errors"));
connectOptions.setExperimentalOption("debuggerAddress", "localhost:4444"); // before this I started Chrome with --remote-debugging-port=4444
ChromeDriver chrome = new ChromeDriver(connectOptions);
browserMobProxy.newHar(); // creating new HAR
chrome.get("https://www.github.com/"); // it navigates in existing chrome instance cool ...
List<HarEntry> entries = browserMobProxy.getHar().getLog().getEntries();
for (HarEntry entry : entries) { // ... but now this collection is empty :-(
System.out.println(entry.getRequest().getUrl());
}
try(OutputStream outputStream = Files.newOutputStream(Path.of(
"C:\\github.test.file.har"), StandardOpenOption.WRITE, StandardOpenOption.CREATE)
) {
browserMobProxy.getHar().writeTo(outputStream);
} catch (Exception ex) {
log.info("Save error = ", ex);
}
browserMobProxy.stop();
}
它连接到端口4444上现有的chrome示例,并导航到“github.com”,我知道bcz我亲眼所见。很遗憾,*.har文件中现在没有数据。想不通,为什么?
暂无答案!
目前还没有任何答案,快来回答吧!