How to Use BrowserMob Proxy with Selenium

Profile picture for user arilio666
Submitted by arilio666 on

Capturing network traffic with Selenium WebDriver in Java necessitates using extra tools and libraries in addition to the core WebDriver capabilities. One typical solution is to employ a proxy server to intercept and analyze the WebDriver's network queries. BrowserMob Proxy is a popular proxy server for this purpose.

To collect network traffic in Java using Selenium WebDriver, follow these steps:

Set up the required dependencies:

The BrowserMob Proxy library (JAR file) can be obtained from the official website or the Maven repository.


Add the BrowserMob Proxy JAR file to the build path of your project or dependency management tool (e.g., Maven or Gradle).

Configure the BrowserMob Proxy server to gather network traffic and start it:

BrowserMobProxy server = new BrowserMobProxyServer();

Set up the WebDriver to use the proxy server as follows:

Proxy seleniumProxy = ClientUtil.createSeleniumProxy(server);
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
    capabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
    EnumSet <CaptureType> captureTypes = CaptureType.getAllContentCaptureTypes();

    ChromeOptions options = new ChromeOptions();
        WebDriver driver = new ChromeDriver(options);

We have adjusted the capture types and passed the capabilities to the driver.

Retrieve the network traffic data that was captured:

        Har har = server.getHar();
        File myHarFile = new File("C:\\Users\\arili\\eclipse-workspace\\mytesting\\target\\PB.har");
        System.out.println("HAR  details successfully written...");

Here we can create a file to write on, and the har file will be made with the necessary traffic logs of the network.

How to Use BrowserMob Proxy with Selenium

Here is the har file that was generated. We used an online har file viewer to view the har file.