Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:SeleniumHQ Selenium DevTools Command

From Leeroopedia
Revision as of 11:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/SeleniumHQ_Selenium_DevTools_Command.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains WebDriver, DevTools_Protocol
Last Updated 2026-02-12 00:00 GMT

Overview

The Command class is a generic container representing a Chrome DevTools Protocol (CDP) command, encapsulating the method name, parameters, and a response mapper function.

Description

Command<X> models a single CDP command that can be sent to the browser through the DevTools session. Each command carries a method name (e.g., "Page.navigate"), an immutable map of parameters, and a Function<JsonInput, X> mapper used to deserialize the JSON response into the expected return type X. The class also supports a sendsResponse flag to handle edge-case CDP commands that do not return a response, via the doesNotSendResponse() factory method.

Usage

Use Command when constructing CDP commands to send via a DevTools session. In most cases, commands are created by the auto-generated CDP domain classes (e.g., Network.enable()), but custom commands can be constructed directly for unsupported or experimental CDP methods.

Code Reference

Source Location

Signature

public class Command<X> {

  public Command(String method, Map<String, Object> params)
  public Command(String method, Map<String, Object> params, Type typeOfX)
  public Command(String method, Map<String, Object> params, Function<JsonInput, X> mapper)

  public String getMethod()
  public Map<String, Object> getParams()
  public boolean getSendsResponse()
  public Command<X> doesNotSendResponse()
}

Import

import org.openqa.selenium.devtools.Command;

I/O Contract

Constructor Parameters

Parameter Type Required Description
method String Yes The CDP method name (e.g., "Page.navigate")
params Map<String, Object> Yes The command parameters as key-value pairs
typeOfX java.lang.reflect.Type No The type to deserialize the response into (defaults to Object.class)
mapper Function<JsonInput, X> No Custom function to deserialize the JSON response

Return Values

Method Return Type Description
getMethod() String The CDP method name
getParams() Map<String, Object> Immutable copy of command parameters
getSendsResponse() boolean Whether the command expects a response from the browser
doesNotSendResponse() Command<X> Returns a new Command instance flagged as not expecting a response

Usage Examples

// Creating a command with a type-based mapper
Command<String> navigateCommand = new Command<>(
    "Page.navigate",
    Map.of("url", "https://example.com"),
    String.class
);

// Creating a command with a custom mapper
Command<Boolean> customCommand = new Command<>(
    "Emulation.setDeviceMetricsOverride",
    Map.of("width", 1024, "height", 768, "mobile", false),
    input -> input.read(Boolean.class)
);

// Sending a command via DevTools session
DevTools devTools = ((HasDevTools) driver).getDevTools();
devTools.createSession();
devTools.send(navigateCommand);

// Handling a command that does not send a response
Command<Void> fireAndForget = new Command<>(
    "SomeMethod",
    Map.of()
).doesNotSendResponse();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment