【问题标题】:BrowserMob with Selenium (Chrome) not loading the web page带有 Selenium (Chrome) 的 BrowserMob 不加载网页
【发布时间】: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


【解决方案1】:

修复了仔细检查你的 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>

【讨论】:

    【解决方案2】:

    同样的问题也发生在我身上。查了一下,原因是代理启动不好或者版本不对。是否更新了 selenium、chrome 和 BMP 版本。它解决了这个问题。以下是我拥有的版本, 请也更新 chrome 安装。

    Chrome 驱动程序

            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.4.0</version>
    

    浏览器暴民

            <groupId>net.lightbody.bmp</groupId>
            <artifactId>browsermob-core</artifactId>
            <version>2.1.5</version>
    

    硒版

            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.7.1</version>
    

    这应该可以解决您的问题。

    【讨论】:

      【解决方案3】:
      BrowserMobProxy proxyBrowser = new BrowserMobProxyServer();
          proxyBrowser.start(0);
          Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyBrowser);
      
          seleniumProxy.setHttpProxy("localhost:"+proxyBrowser.getPort());
          seleniumProxy.setSslProxy("localhost:"+proxyBrowser.getPort());
      

      这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-29
        • 1970-01-01
        • 2017-01-14
        • 1970-01-01
        • 1970-01-01
        • 2015-02-25
        • 1970-01-01
        相关资源
        最近更新 更多