我正在尝试使用示例项目配置BrowserMob以获取网络选项卡数据。但是当我运行脚本时,chrome 不会加载网站,而不是显示消息“没有互联网连接”。在此处输入图像描述
任何帮助将不胜感激 :)
下面是配置:
macOS:10.12.6
Chrome浏览器:61.0.316
使用 Gradle 获取依赖项:
selenium-java:3.4.0 selenium-server:3.4.0 browsermob-core:2.1.4
下面是示例代码:
// start the proxy
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
// start the browser up
System.setProperty("webdriver.chrome.driver", "/Users/abc/Documents/jars/chromedriver");
WebDriver driver = new ChromeDriver(capabilities);
// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
// create a new HAR with the label "google.com"
proxy.newHar("google.com");
// open google.com
driver.get("http://www.google.com");
// get the HAR data
Har har = proxy.getHar();
File harFile = new File("/Users/abc/Documents/Sample.har");
try {
har.writeTo(harFile);
} catch (IOException e) {
e.printStackTrace();
}
driver.quit();
以下是我在 .har 文件中获取的数据。
{"log":{"version":"1.2","creator":{"name":"BrowserMob 代理","version":"2.1.4","comment":""},"pages": [{"id":"google.com","startedDateTime":"2017-11-06T17:33:42.007Z","title":"google.com","pageTimings":{"comment":"" },"comment":""}],"entries":[],"comment":""}}
进口声明:
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.CaptureType;