【发布时间】:2018-04-18 22:56:28
【问题描述】:
我正在尝试使用示例项目配置 BrowserMob 以获取 Network 选项卡数据。但是当我运行脚本时,chrome 不会加载网站,而是显示消息“没有互联网连接”。enter image description here
任何帮助将不胜感激:)
下面是配置:
MacOS:10.12.6
Chrome浏览器:61.0.316
使用 Gradle 获取依赖:
硒-java:3.4.0 硒服务器:3.4.0 browsermob 核心: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 Proxy","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;
【问题讨论】:
-
你能展示你在这个类中导入的内容吗?我的猜测是正在导入错误。目前我刚刚试用了你的代码,它运行良好。试图解决一些常见问题并且它正在工作。
-
@MadisKangro 感谢回复,我已经添加了导入语句。
-
同样的问题,我收到以下错误:ERR_EMPTY_RESPONSE
标签: google-chrome selenium browsermob-proxy