Selenium 4 Chrome DevTools Examples

A few Selenium CDP examples in the Ruby language

Zhimin Zhan
4 min readOct 28, 2021

--

This article is included in my “How to in Selenium WebDriver” series.

Selenium WebDriver 4 supports Chrome DevTools Protocol(CDP), enabling a debugger inside Chromium-based browsers. By the way, Cypress and Puppeteer are based on CDP. CDP has some issues and limitations, not surprisingly, it is a debugging protocol after all.

In this article, I will show some DevTools examples with the new Selenium 4.

Table of Contents:· General
Navigate to a URL
Print to PDF
Get browser Client Size
Get browser info
Execute CDP Command
· Event
Console log messages
· Network
Basic Authentication
· Emulate
GEO Location
· Page
Download file
Navigation history
· Summary

While Selenium 4 supports CDP, it is not included by default. You need to install a separate gem ( ruby library):

> gem install — no-document selenium-devtools
Successfully installed selenium-devtools-0.94.0
1 gem installed

You will see the first limitation, CDP is associated with a particular version of Chrome. For example, I got the following error after the Chrome browser self-updated to v95.

Failure/Error: driver.devtools.page.enable
LoadError:
cannot load such file — selenium/devtools/v95
# ./spec/ch27_chrome_devtools_spec.rb:20

The message was quite clear, updating selenium-devtools gem should fix it. However, the “selenium-devtools-0.95” is not released yet.

General

Navigate to a URL

No difference from driver.get .

driver.devtools.page.navigate(url: “https://travel.agileway.net")
expect(driver.title).to eq(“Agile Travel”)

Print to PDF

Printing a web page to PDF via DevTools works only in headless mode. By the way, Selenium v4 already has save_print_page for this.

driver.get("https://whenwise.com")
data = driver.devtools.page.print_to_pdf
require "base64"
File.open("/tmp/a.pdf","wb").write(Base64.decode64(data["result"]["data"]))

--

--

Zhimin Zhan

Test automation & CT coach, author, speaker and award-winning software developer. Help teams succeed with Agile/DevOps by implementing real Continuous Testing.