2

According to this article, Selenium 4 alpha has a sendDevToolsCommand that sends an arbitrary DevTools command to the browser and returns a promise that will be resolved when the command has finished:

Added “sendDevToolsCommand” and “setDownloadPath” for chrome.Driver.

But I can't seem to find how to use it. It sounds a bit like using JavaScript executor in Selenium.

Can someone provide an example usage? I'm using Selenium + Java.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77

3 Answers3

4

I couldn't find the sendDevToolsCommand in the Selenium documentation yet, but the source actually has the setDownloadPath that you also mentioned above defined right below, which actually uses the sendDevToolsCommand. Based on that usage, it seems like you could do something like:

const { Builder } = require("selenium-webdriver");

const driverInstance = await new Builder()
  .withCapabilities({ browserName: "chrome" })
  .build();

driverInstance.sendDevToolsCommand('Page.setDownloadBehavior', {
  behavior: 'allow',
  downloadPath: path
})

or for a visually obvious example:

await driverInstance.sendDevToolsCommand("Emulation.setDefaultBackgroundColorOverride", {
  color: { r: 0, g: 255, b: 0, a: 1 } // watch out, it's bright!
});

where the first argument is a Chrome Devtools Protocol Domain method (e.g. or Page.setDownloadBehavior or Emulation.setCPUThrottlingRate) and the second argument is an object containing the options for that Domain method (as described in the same protocol docs).

Edit: just tested and the above works :)

I'm excited that this was added because it means that, in addition to network throttling, it should be pretty trivial to add cpu throttling to Selenium tests now! Something like:

driverInstance.sendDevToolsCommand('Emulation.setCPUThrottlingRate', {
  rate: 4 // throttle cpu 4x
}
apeltz
  • 71
  • 1
  • 5
4

Selenium 4 release will have a user friendly API for Chrome DevTools protocol. I just finished implementing Network and Performance domains for the Selenium Java client. https://github.com/SeleniumHQ/selenium/pull/7212

In addition, there is a generic API for all domains in Java client that was merged a while ago. All those new features will be released probably in the next Alpha release.

Adi Ohana
  • 927
  • 2
  • 13
  • 18
4

The command to call the devtool api was added a few years back in the Chrome driver.

You can already use it with Selenium even if the method is not yet present:

This command gives you access to the devtools api, which is used by ChromeDriver internally to drive the browser.

The method takes the name of the command as first argument and a dictionary of parameters as second argument. To figure out how to call a command, add puppeteer in your searches. For instance puppeteer set download location.

Note that executeCdpCommand is implemented in the Java master branch, so it should be available in the next release.

Florent B.
  • 41,537
  • 7
  • 86
  • 101