Selenium Intercept Requests

Profile picture for user arilio666

With the latest selenium in the pack, it has introduced the concept of CDP, where it has all the access to browser network features. This includes network call requests made during entry. This article will show how we can intercept requests using Selenium 4 Java. Let us see how we can capture/intercept network requests using Selenium 4.

1. Initiate CDP Tools

We need to create an instance using getDevTools from the driver. Only here can we access the CDP features.

        System.setProperty("webdriver.chrome.driver","C:\\Users\\arili\\eclipse-workspace\\mytesting\\target\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");

        WebDriver driver = new ChromeDriver(options);

        driver.manage().window().maximize();    
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        
        org.openqa.selenium.devtools.DevTools devTool = ((HasDevTools) driver).getDevTools();
        
                devTool.createSession();

Here we have created the driver instance, and from that, we created a devTool instance to access the CDP feature and create a session out of it.

2. Network

Once that is done, we will enable the network and add a listener to requestWillBeSent.

devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
       devTool.addListener(Network.requestWillBeSent(), requestSent -> {
             System.out.println("URL => " + requestSent.getRequest().getUrl());
             System.out.println("Method => " + requestSent.getRequest().getMethod());
             System.out.println("Headers => " + requestSent.getRequest().getHeaders().toString());
             System.out.println("------------------------------------------------------");
       });
        
  • So here, we have enabled the network and then added a listener.
  • We can now fetch our choice's URL, method, and headers from the requested argument.
  • Once this part is done, we can navigate to the site. For this, we will be using the programsbuzz site.
package com.pb.test.mytesting;
import java.time.Duration;
import java.util.HashMap;
import java.util.Optional;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.v113.network.Network;
import org.openqa.selenium.devtools.v113.network.model.Request;
public class SelReq {
    
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\arili\\eclipse-workspace\\mytesting\\target\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");

        WebDriver driver = new ChromeDriver(options);

        driver.manage().window().maximize();    
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        
        org.openqa.selenium.devtools.DevTools devTool = ((HasDevTools) driver).getDevTools();

       devTool.createSession();
       
       devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
       devTool.addListener(Network.requestWillBeSent(), requestSent -> {
             System.out.println("URL => " + requestSent.getRequest().getUrl());
             System.out.println("Method => " + requestSent.getRequest().getMethod());
             System.out.println("Headers => " + requestSent.getRequest().getHeaders().toString());
             System.out.println("------------------------------------------------------");
       });
       driver.get("https://www.programsbuzz.com");
    }
}

Selenium Intercept Requests