【问题标题】:How to set BrowserMob with WebDriver?如何使用 WebDriver 设置 BrowserMob?
【发布时间】:2014-11-09 04:23:35
【问题描述】:

我正在尝试拦截 Webdriver 使用 BrowserMobProxy 发出的请求。

但下面的代码不起作用..它无法打开网站 google.com。

它说“Internet Explorer 无法打开该站点”

    proxyServer = new ProxyServer(9101);
    proxyServer.start();

    proxyServer.setCaptureHeaders(true);
    proxyServer.setCaptureContent(true);

    proxyServer.addRequestInterceptor(new RequestInterceptor() {
        @Override
        public void process(BrowserMobHttpRequest request, Har har) {
            System.out.println("From Process method");
        }
    });

    seleniumProxy = proxy.seleniumProxy();

    seleniumProxy.setHttpProxy("localhost:9101");

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
    capabilities.setCapability("ie.setProxyByServer", true);

    File file = new File("C:\\path\\IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());

    driver = new InternetExplorerDriver(capabilities);
    driver.get("www.google.com");

我在尝试从 webdrvover 访问 google.com 时收到以下错误

From Process method Nov 09, 2014 2:07:58 AM net.lightbody.bmp.proxy.util.Log info INFO: java.net.UnknownHostException: www.google.com when requesting http://www.google.com/

【问题讨论】:

  • get 方法调用中使用实际 URL 会发生什么?换句话说,当你使用driver.get("http://www.google.com"); 时,会发生什么? get 方法仅适用于完整的 URL(包括协议方案),而不仅仅是地址。
  • 吉姆,我尝试了您的解决方案,但仍然失败。这只发生在我连接 VPN 的 WiFi 连接时。否则它工作正常。关于如何解决这个问题的任何想法?我在这里也发布了同样的问题groups.google.com/forum/#!topic/browsermob-proxy/QRetOsrQFjk

标签: java webdriver browsermob


【解决方案1】:

Browsermob 使用 xbill DNS 而不是常规的 Java/本机 DNS 解析,这可能无法与您的 VPN 配合使用。最新的 browsermob 快照允许您通过将系统属性 bmp.allowNativeDnsFallback 设置为 true 来启用本机 DNS 回退:

System.setProperty("bmp.allowNativeDnsFallback", "true");
proxyServer = new ProxyServer(9101);
proxyServer.start();

您可以通过browsermob github page获取最新快照。

【讨论】:

  • 感谢您的回答。我会试试这个。
  • 这对你有用吗?如果没有,您可以在 browsermob github 页面创建一个新问题。
猜你喜欢
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
相关资源
最近更新 更多