0

尝试使用 PhantomJS(com.codeborne:phantomjsdriver:1.2.1) 和 BrowserMob Proxy(browsermob-proxy-2.0-beta-9) 进行一些无头浏览器测试,以捕获 HAR 文件和 Javascript 执行。

它适用于带有 https 的网址(例如https://www.google.com),我得到了 HAR。
但是对于 http(例如http://www.google.com),我在 BrowserMob 日志中收到以下错误

INFO 02/02 22:45:03 n.l.b.p.j.h.HttpSer~ - Version Jetty/5.1.x
INFO 02/02 22:45:03 n.l.b.p.j.u.Contain~ - Started HttpContext[/,/]
...
INFO 02/02 22:46:29 n.l.b.p.h.BrowserMo~ - java.net.UnknownHostException: www.google.com when requesting http://www.google.com/
INFO 02/02 22:46:54 n.l.b.p.j.u.Threade~ - Stopping Acceptor ServerSocket[addr=/0.0.0.0,localport=13000]
...

以下是我设置 PhantomJS 的方式

public RemoteWebDriver getDriverInstance() {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setJavascriptEnabled(true);

    //code to get Proxy is below
    capabilities.setCapability(CapabilityType.PROXY, getProxyObject()); 

    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "./bin/phantomjs");
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ssl-protocol=any", "--ignore-ssl-errors=yes"});

    WebDriver webDriver = new PhantomJSDriver(capabilities);
    return (RemoteWebDriver) webDriver;
}

public Proxy getProxyObject() {
    Proxy proxy = new Proxy();

    //publicIp is localhost for testing purposes.
    String proxyLocation = this.getPublicIp() + ":" + this.getBrowserMobProxyPort();

    proxy.setHttpProxy(proxyLocation);
    proxy.setFtpProxy(proxyLocation);
    proxy.setSslProxy(proxyLocation);
    return proxy;
}

仍在寻找解决方案。
期待来自 BrowserMob 的此类消息是否正常?

我很可能没有正确设置某些东西或错过了一部分。如果有人遇到此问题,请帮助我或为我指出解决方案,那将是很棒的。我进行了一些搜索,但没有找到解决此问题的解决方案。

另外,如果需要其他信息,请告诉我。

4

1 回答 1

0

我正在使用较新版本的 BrowserMob 代理,但以下 Scala 代码适用于我加载 HTTP 和 HTTPS 网站:

import java.io.File

import net.anthavio.phanbedder.Phanbedder
import net.lightbody.bmp.BrowserMobProxyServer
import net.lightbody.bmp.client.ClientUtil
import net.lightbody.bmp.core.har.HarEntry
import org.apache.commons.io.FileUtils
import org.openqa.selenium.OutputType
import org.openqa.selenium.phantomjs.PhantomJSDriver
import org.openqa.selenium.phantomjs.PhantomJSDriverService._
import org.openqa.selenium.remote.{CapabilityType, DesiredCapabilities}

import scala.collection.JavaConversions._

object PhantomJSTest {
  def main(args: Array[String]) {
    val bm = new BrowserMobProxyServer
    bm.start(0)

    val proxy = ClientUtil.createSeleniumProxy(bm)

    val phantomjs = Phanbedder.unpack()
    val capabilities = new DesiredCapabilities
    capabilities.setCapability(CapabilityType.PROXY, proxy)
    capabilities.setCapability(PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                               phantomjs.getAbsolutePath())
    capabilities.setCapability(PHANTOMJS_CLI_ARGS,
      Array[String]("--web-security=no",
                    "--ssl-protocol=any",
                    "--ignore-ssl-errors=yes"))

    val driver = new PhantomJSDriver(capabilities)

    run(bm, driver, "http://www.google.com")
    run(bm, driver, "https://www.google.com")

    driver.quit
    bm.stop
  }

  def run(bm: BrowserMobProxyServer, driver: PhantomJSDriver, url: String) {
    bm.newHar(url)
    driver.get(url)

    val har = bm.getHar
    har.getLog.getEntries.foreach { e: HarEntry =>
      println(e.getRequest.getUrl)
    }

    val file = new File(s"screenshot-${System.currentTimeMillis}.png")
    FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), file)
    println(s"Captured loading of ${url} screenshot to ${file.getCanonicalPath}")
  }
}

这是我正在使用的库(来自我的build.gradle文件):

compile 'commons-io:commons-io:2.4'
compile 'org.slf4j:slf4j-simple:1.7.16'
compile 'net.lightbody.bmp:browsermob-core-littleproxy:2.1.0-beta-4'
compile 'org.seleniumhq.selenium:selenium-java:2.45.0'
compile 'com.codeborne:phantomjsdriver:1.2.1'
compile 'net.anthavio:phanbedder-2.1.1:1.0.0'

希望这可以帮助您调试遇到的问题。

于 2016-02-24T07:38:31.933 回答