I would like to capture the network traffic generated in a Chromedriver window. I have found out that it can be done using selenium 4.0 DevTools utility but I can´t find how to or a good documentation.
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/devtools/DevTools.html
Is there an easiest way to do? Thanks

Using Selenium 4 you can get requests URL and Response URLs and more.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.3.1</version>
</dependency>
Using this snippet you will get all requests and responses.
@BeforeEach
public void setUp() {
WebDriverManager.chromedriver().setup();
this.chromeDriver = new ChromeDriver();
devTools = chromeDriver.getDevTools();
devTools.createSession();
}
@Test
public void getRequestsAndResponseUrls() throws InterruptedException {
devTools.send(new Command<>("Network.enable", ImmutableMap.of()));
;
devTools.addListener(Network.responseReceived(), l -> {
System.out.print("Response URL: ");
System.out.println(l.getResponse().getUrl());
});
devTools.addListener(Network.requestWillBeSent(), l -> {
System.out.print("Request URL: ");
System.out.println(l.getRequest().getUrl());
});
chromeDriver.get("https://edition.cnn.com/");
// While Thread.sleep you you will see requests and responses appearing in console.
Thread.sleep(10000);
}
Enjoy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With