Search
  • Money Tips | Product Reviews

Selenium 4 Java and Python - Chrome DevTools to capture XHR response body

Updated: 2 days ago


Selenium 4 (Java, Python, C# and other languages) has now reached beta and version 4 comes with support for Chrome DevTools. This tool is very useful for capturing Network XHR response and many other features of chromium-based browsers (Chrome) such as Application Cache, Fetch, Network, Performance, Profiler, Resource Timing, Security, and Target CDP domains, etc.


Selenium 4 DevTools:

How to capture XHR response and more from the Network panel?

To capture the XHR response, follow these steps:

Step 1:

Add the following dependency to your pom.xml for your Maven project


<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>4.0.0-beta-3</version>

</dependency>


build.gradle for gradle project

implementation group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '4.0.0-beta-3'


Step 2:


In your Selenium test, add this code for capturing the XHR response you see in the Chrome Network tab.


String xhrResponse;

DevTools() devTools = ((ChromeDriver)driver).getDevTools();

devTools.createSession();

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

//add listener for responseBody

devTools.addListener(Network.responseReceived(), response ->

{response.getRequestId

xhrResponse = devTools.send(Network.getResponse().body;

});

System.out.println("xhrResponse is: " + xhrResponse );


There seems to be a javascript way of doing the same as explained here. But for your automation test, using DevTools is pretty straightforward and I recommend using this tool.


Read more about Selenium support for Chrome DevTools here.


Most popular:

How To Record Using JMeter Chrome Extension

Drivers in the US earn $500 or more every year on gas with this free cashback app



Recommended