2

As we know One of the features added in the new version of Selenium (4.0.0-alpha-2) is a very nice Interface for Chrome DevTools API in Java.DevTools API offers great capabilities for controlling the Browser and the Web Traffic

As per documentation using the latest version of selenium we can capture the network request from the session.

Before I used browsermob for getting the network request but unfortunately they didn't update it a couple of years.

I am looking for someone who used this selenium4 dev tools API for getting all the internal request.

Can anyone suggest me how can I start to get all the requests? Thanks, advance

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52

1 Answers1

1

You can find @adiohana's example in the selenium-chrome-devtools-examples repo on gitHub.

I think youed find this test example helpful:

public class ChromeDevToolsTest {

    private static ChromeDriver chromeDriver;
    private static DevTools chromeDevTools;

    @BeforeClass
    public static void initDriverAndDevTools() {

        chromeDriver = new ChromeDriver();
        // dev-tools handler
        chromeDevTools = chromeDriver.getDevTools();
        chromeDevTools.createSession();

    }

    @Test
    public void interceptRequestAndContinue() {

        //enable Network
        chromeDevTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        //add listener to intercept request and continue
        chromeDevTools.addListener(Network.requestIntercepted(),
                requestIntercepted -> chromeDevTools.send(
                        Network.continueInterceptedRequest(requestIntercepted.getInterceptionId(),
                                Optional.empty(),
                                Optional.empty(),
                                Optional.empty(), Optional.empty(),
                                Optional.empty(),
                                Optional.empty(), Optional.empty())));

        //set request interception only for css requests
        RequestPattern requestPattern = new RequestPattern("*.css", ResourceType.Stylesheet, InterceptionStage.HeadersReceived);
        chromeDevTools.send(Network.setRequestInterception(ImmutableList.of(requestPattern)));

        chromeDriver.get("https://apache.org");

    }

You need to add the following imports:

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Console;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.network.Network;
import org.openqa.selenium.devtools.network.model.BlockedReason;
import org.openqa.selenium.devtools.network.model.InterceptionStage;
import org.openqa.selenium.devtools.network.model.RequestPattern;
import org.openqa.selenium.devtools.network.model.ResourceType;
import org.openqa.selenium.devtools.security.Security;

import java.util.Optional;
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38