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:
Add the following dependency to your pom.xml for your Maven project
build.gradle for gradle project
implementation group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '4.0.0-beta-3'
In your Selenium test, add this code for capturing the XHR response you see in the Chrome Network tab.
DevTools() devTools = ((ChromeDriver)driver).getDevTools();
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
//add listener for responseBody
devTools.addListener(Network.responseReceived(), response ->
xhrResponse = devTools.send(Network.getResponse().body;
System.out.println("xhrResponse is: " + xhrResponse );
Read more about Selenium support for Chrome DevTools here.