Chrome DevTools and Selenium 4

Adi Ohana
2 min readJul 3, 2019

--

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.

In this post, we will review the benefits we can gain from using the DevTools API and will go over some basic examples for some of the capabilities.

DevTools API offers great capabilities for controlling the Browser and the Web Traffic. The complete API can be found here: https://chromedevtools.github.io/devtools-protocol/

Here is a list of some of the things we can achieve by using DevTools API with Selenium:

  • URL filtering
  • Adding custom headers for requests
  • Intercepting requests/responses and acting like a “Proxy”
  • Get performance and Metrics of our Browser/Network
  • Leverage Console capabilities
  • Emulate network conditions
  • Perform security operations

Now, let’s get dirty with some code:

First, make sure you have the latest Selenium version from Maven

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-2</version>
</dependency>

Now, let’s init our ChromeDriver and create a new DevTools session:

URL Filtering

Adding Custom Headers

Intercepting requests

Listen To Console Logs

IgnoreCertificateErrors

In general, Selenium Java client supports all DevTools APIs and can be implemented by using “Command” and “Event”. Here is an example for crashing the Chrome instance:

Full project can be found on my github:

I believe that Selenium 4 RELEASE will contain the user-friendly interface for most (maybe all) DevTools command and events.

--

--