BrowserMob代理+ Selenium:未收到任何HTTP响应 [英] BrowserMob Proxy + Selenium: Not receiving any HTTP responses

查看:733
本文介绍了BrowserMob代理+ Selenium:未收到任何HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有依赖项的Maven / Java项目:

Maven/Java project with dependencies:

      <dependencies>
<dependency>
  <groupId>net.lightbody.bmp</groupId>
  <artifactId>browsermob-core</artifactId>
  <version>2.1.4</version>
  <scope>test</scope>
</dependency>


<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.3.4</version>
</dependency>

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

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>23.0</version>
</dependency>
</dependencies>

代码:

公共类bmp_Selenium {

public class bmp_Selenium {

@Test
public void test() throws Exception {
    String chromedriverPath = System.getProperty("user.dir") + "/chromedriver";
    System.setProperty("webdriver.chrome.driver", chromedriverPath);


    BrowserMobProxy proxy = getProxyServer(); //getting browsermob proxy
    System.out.println("BrowserMob Proxy running on port: " + proxy.getPort());

    Proxy seleniumProxy = getSeleniumProxy(proxy);

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

    WebDriver driver = new ChromeDriver(capabilities);

    proxy.newHar("foo");
    driver.get("http://www.google.com");


    Har har = proxy.getHar();

    // Write HAR Data in a File
    String harFilePath = System.getProperty("user.dir") + "/hars/ww.har";
    File harFile = new File(harFilePath);
    try {
        har.writeTo(harFile);
    } catch (IOException ex) {
         System.out.println (ex.toString());
         System.out.println("Could not find file " + harFilePath);
    }

    List<HarEntry> entries = har.getLog().getEntries();
    for (HarEntry entry : entries) {
        System.out.println("Request URL: " + entry.getRequest().getUrl());
        System.out.println("Entry response status: " + entry.getResponse().getStatus());
        System.out.println("Entry response text: " + entry.getResponse().getStatusText());

    }


    proxy.stop();
    driver.quit();
}


public Proxy getSeleniumProxy(BrowserMobProxy proxyServer) {
    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);
    try {
        String hostIp = Inet4Address.getLocalHost().getHostAddress();
        seleniumProxy.setHttpProxy(hostIp + ":" + proxyServer.getPort());
        seleniumProxy.setSslProxy(hostIp + ":" + proxyServer.getPort());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        Assert.fail("invalid Host Address");
    }
    return seleniumProxy;
}

public BrowserMobProxy getProxyServer() {
    BrowserMobProxy proxy = new BrowserMobProxyServer();
    proxy.setTrustAllServers(true);
    proxy.setHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
    proxy.start();
    return proxy;
}

}

控制台输出:


在端口上运行的浏览器代理:57547
启动ChromeDriver 2.33.506106(8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2 )在端口47157
上仅允许本地连接。
2017年11月30日下午4:48:33 org.openqa.selenium.remote.ProtocolHandshake createSession
信息:检测到方言:OSS
请求URL: http://www.google.com/
输入响应状态:0
输入响应文本:
请求网址: http://www.google.com/
输入响应状态:0
输入响应文本:
请求URL: http://www.google.com/
输入响应状态:0
输入响应文本:
请求URL: http:/ /www.google.com/
输入响应状态:0
输入响应文本:

BrowserMob Proxy running on port: 57547 Starting ChromeDriver 2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2) on port 47157 Only local connections are allowed. Nov 30, 2017 4:48:33 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS Request URL: http://www.google.com/ Entry response status: 0 Entry response text: Request URL: http://www.google.com/ Entry response status: 0 Entry response text: Request URL: http://www.google.com/ Entry response status: 0 Entry response text: Request URL: http://www.google.com/ Entry response status: 0 Entry response text:

Har File:

{
  "log": {
    "version": "1.2",
    "creator": {
      "name": "BrowserMob Proxy",
      "version": "2.1.4",
      "comment": ""
    },
    "pages": [{
        "id": "foo",
        "startedDateTime": "2017-11-30T21:48:34.064Z",
        "title": "foo",
        "pageTimings": {
          "comment": ""
        },
        "comment": ""
      }
    ],
    "entries": [{
        "pageref": "foo",
        "startedDateTime": "2017-11-30T21:48:34.210Z",
        "request": {
          "method": "GET",
          "url": "http://www.google.com/",
          "httpVersion": "HTTP/1.1",
          "cookies": [

          ],
          "headers": [

          ],
          "queryString": [

          ],
          "headersSize": 417,
          "bodySize": 0,
          "comment": ""
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "cookies": [

          ],
          "headers": [

          ],
          "content": {
            "size": 0,
            "mimeType": "",
            "comment": ""
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "comment": "",
          "_error": "No response received"
        },
        "cache": {},
        "timings": {
          "comment": "",
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1,
          "connect": -1,
          "blocked": 0,
          "dns": -1
        },
        "comment": "",
        "time": 0
      }, {
        "pageref": "foo",
        "startedDateTime": "2017-11-30T21:48:34.224Z",
        "request": {
          "method": "GET",
          "url": "http://www.google.com/",
          "httpVersion": "HTTP/1.1",
          "cookies": [

          ],
          "headers": [

          ],
          "queryString": [

          ],
          "headersSize": 417,
          "bodySize": 0,
          "comment": ""
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "cookies": [

          ],
          "headers": [

          ],
          "content": {
            "size": 0,
            "mimeType": "",
            "comment": ""
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "comment": "",
          "_error": "No response received"
        },
        "cache": {},
        "timings": {
          "comment": "",
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1,
          "connect": -1,
          "blocked": 0,
          "dns": -1
        },
        "comment": "",
        "time": 0
      }, {
        "pageref": "foo",
        "startedDateTime": "2017-11-30T21:48:34.225Z",
        "request": {
          "method": "GET",
          "url": "http://www.google.com/",
          "httpVersion": "HTTP/1.1",
          "cookies": [

          ],
          "headers": [

          ],
          "queryString": [

          ],
          "headersSize": 417,
          "bodySize": 0,
          "comment": ""
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "cookies": [

          ],
          "headers": [

          ],
          "content": {
            "size": 0,
            "mimeType": "",
            "comment": ""
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "comment": "",
          "_error": "No response received"
        },
        "cache": {},
        "timings": {
          "comment": "",
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1,
          "connect": -1,
          "blocked": 0,
          "dns": -1
        },
        "comment": "",
        "time": 0
      }, {
        "pageref": "foo",
        "startedDateTime": "2017-11-30T21:48:34.327Z",
        "request": {
          "method": "GET",
          "url": "http://www.google.com/",
          "httpVersion": "HTTP/1.1",
          "cookies": [

          ],
          "headers": [

          ],
          "queryString": [

          ],
          "headersSize": 443,
          "bodySize": 0,
          "comment": ""
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "cookies": [

          ],
          "headers": [

          ],
          "content": {
            "size": 0,
            "mimeType": "",
            "comment": ""
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "comment": "",
          "_error": "No response received"
        },
        "cache": {},
        "timings": {
          "comment": "",
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1,
          "connect": -1,
          "blocked": 0,
          "dns": -1
        },
        "comment": "",
        "time": 0
      }
    ],
    "comment": ""
  }
}


推荐答案

根据GitHub用户mediga的建议,修改BMP依赖关系解决了该问题:

As per GitHub user mediga's suggestion, modifying the BMP dependency resolved the issue:

替换此

<dependency>
  <groupId>net.lightbody.bmp</groupId>
  <artifactId>browsermob-core</artifactId>
  <version>2.1.4</version>
  <scope>test</scope>
</dependency>

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

https://github.com/lightbody/browsermob-proxy/issues/689#issuecomment-348620752

这篇关于BrowserMob代理+ Selenium:未收到任何HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆