简体   繁体   English

尝试在 Java 中使用 Browsermob + Selenium 读取请求标头,但只得到空的 har

[英]Tried reading Request Headers with Browsermob + Selenium in Java but only got empty har

My goal is to read the request headers of the app portal.我的目标是阅读应用程序门户的请求标头。
Am using browsermob-core with selenium, but the entries in har always comes empty.我将 browsermob-core 与 selenium 一起使用,但 har 中的条目始终为空。
I tried removing the headless arg and also tried using a separate SSLProxy for BMP, but still the har is empty.我尝试删除无头 arg 并尝试为 BMP 使用单独的 SSLProxy,但 har 仍然是空的。 Heres my code:这是我的代码:

package com.example;

import io.github.bonigarcia.wdm.WebDriverManager;
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.core.har.HarEntry;
import net.lightbody.bmp.proxy.CaptureType;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class webDriver {
    private static final Logger logger
            = LoggerFactory.getLogger(webDriver.class);
            //= LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);

    public static void generateUIToken() throws Exception {
        
        WebDriver driver = null;
        BrowserMobProxy proxy = new BrowserMobProxyServer();
        proxy.setTrustAllServers(true);
        proxy.start(0);
        Har har;
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
        String hostIp = "localhost";
        seleniumProxy.setHttpProxy(hostIp + ":" + proxy.getPort());
        seleniumProxy.setSslProxy(hostIp + ":" + proxy.getPort());
        seleniumProxy.setSslProxy("trustAllSSLCertificates");

        WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--no-sandbox");
        options.addArguments("--headless");
        options.addArguments("--ignore-certificate-errors");
        options.addArguments("--disable-dev-shm-usage");

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
        capabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS,true);
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
        options.merge(capabilities);

        try {

            driver = new ChromeDriver(options);



            proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

            proxy.setHarCaptureTypes(CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_HEADERS);
            proxy.enableHarCaptureTypes(CaptureType.REQUEST_HEADERS,CaptureType.RESPONSE_HEADERS);
            proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
            proxy.newHar("new.example.com");

            driver.get("https://new.example.com");

            String title = driver.getTitle();
            TimeUnit.SECONDS.sleep(10);
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
            wait.until(ExpectedConditions.elementToBeClickable(By.id("userInput")));
            WebElement user_login = driver.findElement(By.id("userInput"));
            user_login.sendKeys("username");
            user_login.submit();
            logger.info(user_login + " - " + title);
            TimeUnit.SECONDS.sleep(10);

            wait.until(ExpectedConditions.elementToBeClickable(By.id("okta-signin-password")));
            WebElement pass_login = driver.findElement(By.id("okta-signin-password"));
            pass_login.sendKeys("password");
            pass_login.submit();
            logger.info(pass_login + " - " + title);

            TimeUnit.SECONDS.sleep(30);

            logger.info("Current" + driver.getCurrentUrl());
            har = proxy.getHar();
            List<HarEntry> entries = har.getLog().getEntries();
            for (HarEntry entry : entries) {
                logger.info("Request URL: " + entry.getRequest().getUrl());
                logger.info("Entry response status: " + entry.getResponse().getStatus());
                logger.info("Entry response text: " + entry.getResponse().getStatusText());

            }
            har.writeTo(new File("test.har"));
            TimeUnit.SECONDS.sleep(10);
            driver.quit();
            proxy.endHar();
            proxy.stop();

        }
        catch (Exception e){
            assert driver != null;
            driver.close();
            driver.quit();
        }
    }

    public static void main(String[] args) throws Exception {

        generateUIToken();


    }
}

And heres the output in har file:这是 har 文件中的 output:

{"log":{"version":"1.2","creator":{"name":"BrowserMob Proxy","version":"2.1.5","comment":""},"pages":[{"id":"new.example.com","startedDateTime":"2022-12-06T21:46:04.043Z","title":"new.example.com","pageTimings":{"comment":""},"comment":""}],"entries":[],"comment":""}}

Am using Java 17 and browsermob-core 2.1.5 with Selenium 4.7.0我正在使用 Java 17 和 browsermob-core 2.1.5 以及 Selenium 4.7.0
Can anyone help me figure out why the har is always having empty entries?谁能帮我弄清楚为什么 har 总是有空条目?

Its exactly not answer your question but your problem might solve in other way.它完全不能回答您的问题,但您的问题可能会以其他方式解决。 Selenium can intercept.network and get request and response header. In selenium have 4.0.6 have that capablity. Selenium 可以拦截.network 并获取请求和响应 header。在 selenium 中有 4.0.6 具有该功能。

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

</dependency>

Here is the full code for how to intercept这是如何拦截的完整代码

public static void main(String[] args) throws InterruptedException, AWTException {

    ChromeOptions options = new ChromeOptions();

    ChromeDriver driver = new ChromeDriver(options);
    driver.get("https://www.google.com");
    DevTools devTools = driver.getDevTools();
    devTools.createSession();

    devTools.send(Network.enable(Optional.empty(),Optional.empty(),Optional.empty()));

    devTools.addListener(Network.requestWillBeSent(),
            request ->{
                System.out.println("Request URL:"+request.getRequest().getUrl());
                System.out.println("Request Method:"+request.getRequest().getMethod());
                System.out.println("Request Method:"+request.getRequest().getHeaders().toJson());
            });
    Thread.sleep(50000);
}

Now you can save all request inside a file a JSON or har extension现在您可以将所有请求保存在文件中,扩展名为 JSON 或 har

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM