通过硒中的browsermob代理运行时,Har文件始终返回空

时间:2019-08-02 10:49:34

标签: java selenium selenium-webdriver browsermob-proxy browsermob

我试图通过browsermob代理读取硒重放期间遍历的页面源,但是我总是得到一个空的har文件。

硒3.141 browsermob-core 2.1.5 jar

下面是我的代码。

如果我设置

WebDriver驱动程序=新的C​​hromeDriver(选项);页面被加载

WebDriver驱动程序=新的C​​hromeDriver(功能);页面无法加载,返回页面未加载

代理已根据日志正确启动

/ *********************************** /

[RemoteTestNG]检测到TestNG版本6.14.3

SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”。

SLF4J:默认为不操作(NOP)记录器实现

SLF4J:有关更多详细信息,请参见http://www.slf4j.org/codes.html#StaticLoggerBinder

net.lightbody.bmp.BrowserMobProxyServer@61322f9dBrowserMobProxy 59858是端口号

Proxy(手动,http = 10.88.16.64:59858,ssl = 10.88.16.64:59858)seleniumProxy

/ ************************************ /

任何建议都会有所帮助。

public void test() throws Exception 
    {

        BrowserMobProxy proxy = getProxyServer(); //getting browsermob proxy
        System.out.println(proxy+"BrowserMobProxy");
        Proxy seleniumProxy = getSeleniumProxy(proxy);
        System.out.println(seleniumProxy+"seleniumProxy");
        DesiredCapabilities capabilities = new DesiredCapabilities().chrome();


        capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        System.setProperty("webdriver.chrome.driver", "C:\\APMWS\\testmvnproject\\src\\drivers\\chromedriver76.exe");
         ChromeOptions options = new ChromeOptions();
         WebDriver driver = new ChromeDriver(options);

        proxy.newHar(); // creating new HAR
        driver.get("https://google.com");
        driver.navigate().to("https://pizzahut.com"); //example

        List<HarEntry> entries = proxy.getHar().getLog().getEntries();
        for (HarEntry entry : entries) 
        {
            HarRequest request = entry.getRequest();
            HarResponse response = entry.getResponse();
            System.out.println(request.getUrl()+" : "+response.getStatus()+","+entry.getTime()+"ms");
        }
    proxy.stop();
    driver.close();
    }

    public Proxy getSeleniumProxy(BrowserMobProxy proxyServer) 
    {
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);
        System.out.println(seleniumProxy.getHttpProxy());
    try
    {
        String hostIp = Inet4Address.getLocalHost().getHostAddress();
        System.out.println(hostIp);
        seleniumProxy.setHttpProxy(hostIp + ":" + proxyServer.getPort());
        seleniumProxy.setSslProxy(hostIp + ":" + proxyServer.getPort());
        System.out.println(proxyServer.getPort()+"is port number");
    } 
    catch (UnknownHostException e) 
    {
        e.printStackTrace();
        System.out.println("invalid host");
    }
    return seleniumProxy;
}

public BrowserMobProxy getProxyServer() 
{
    BrowserMobProxy proxy = new BrowserMobProxyServer();
    proxy.setTrustAllServers(true);
    proxy.start(0);
    return proxy;
}

预期结果:将通过代理运行的跟踪写入HAR文件

实际结果:设置了代理,启动了页面,但HAR文件为空。

1 个答案:

答案 0 :(得分:0)

您可以摆脱以下情况: SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”。

如果您正在运行Maven,只需将其放入POM文件中。

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.25</version>
  <scope>test</scope>
</dependency>

这就是我在Chrome中启动Har文件的方式。

  // start the proxy
        proxy = new BrowserMobProxyServer();
        proxy.start(0);
        // get the Selenium proxy object
        org.openqa.selenium.Proxy seleniumProxy = 
        ClientUtil.createSeleniumProxy(proxy);
        ChromeOptions options = new ChromeOptions();
        options.setCapability(CapabilityType.PROXY, seleniumProxy);
        options.setExperimentalOption("useAutomationExtension", false);
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);