我正在尝试使用示例项目配置 BrowserMob 以获取网络标签数据。但是当我运行脚本时,chrome并没有加载网站,而是显示消息说" 没有互联网连接"。enter image description here
任何帮助将不胜感激:)
以下是配置:
MacOS:10.12.6
ChromeBrowser: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文件中获取的数据。
{"登录" {"版本":" 1.2""创建器" {"名称&#34 ;:" BrowserMob代理","版本":" 2.1.4","评论":""} "页面":[{" ID":" google.com"" startedDateTime":" 2017-11-06T17 :33:42.007Z""标题":" google.com"" pageTimings" {"评价":&# 34;"}"评价":""}],"条目":[],"评价":& #34;"}}
导入声明:
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;
答案 0 :(得分:0)
修复双重检查你的POM我是2.1.4后面的浏览器暴徒版本,改为:我看到你在GitHub上使用Gradle他们在2.1.5。
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
<scope>test</scope>
</dependency>
答案 1 :(得分:0)
同样的问题也发生在我身上。搜索并找到了代理没有启动或版本不正确的原因。更新了硒,铬和BMP版本。它解决了这个问题。以下是我的版本, 请更新chrome安装。
Chrome驱动程序
<artifactId>selenium-chrome-driver</artifactId>
<version>3.4.0</version>
Browser Mob
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
Selenium Version
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
这可以解决您的问题。
答案 2 :(得分:0)
BrowserMobProxy proxyBrowser = new BrowserMobProxyServer();
proxyBrowser.start(0);
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyBrowser);
seleniumProxy.setHttpProxy("localhost:"+proxyBrowser.getPort());
seleniumProxy.setSslProxy("localhost:"+proxyBrowser.getPort());
这对我有用。