Chrome DevTools Protocol version 99 support library for Selenium WebDriver Java bindings
—
JavaScript execution, debugging, and browser-side binding management for advanced automation scenarios. Provides capabilities for script injection, JavaScript binding creation, and runtime interaction.
import org.openqa.selenium.devtools.v99.V99Javascript;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;
import java.util.function.Consumer;Creates a JavaScript operations handler for Chrome DevTools Protocol v99.
/**
* Creates JavaScript operations handler for CDP v99
* @param devtools - DevTools client instance
*/
public V99Javascript(DevTools devtools);Enable and disable the JavaScript runtime domain for script execution and debugging.
/**
* Enable JavaScript runtime domain for script operations
* @return Command to enable runtime
*/
protected Command<Void> enableRuntime();
/**
* Disable JavaScript runtime domain
* @return Command to disable runtime
*/
protected Command<Void> disableRuntime();Enable and disable the page domain for script injection on new documents.
/**
* Enable page domain for script injection capabilities
* @return Command to enable page domain
*/
protected Command<Void> enablePage();
/**
* Disable page domain
* @return Command to disable page domain
*/
protected Command<Void> disablePage();Create and manage communication channels between browser JavaScript and Java code.
/**
* Add JavaScript binding for browser-to-Java communication
* @param scriptName - Name of the binding function in JavaScript
*/
public void addJsBinding(String scriptName);
/**
* Remove JavaScript binding
* @param scriptName - Name of the binding to remove
*/
public void removeJsBinding(String scriptName);
/**
* Add listener for when JavaScript bindings are called
* @param listener - Consumer that receives payload strings from JavaScript
*/
public void addBindingCalledListener(Consumer<String> listener);
/**
* Add JavaScript binding for browser-to-Java communication (internal)
* @param scriptName - Name of the binding function in JavaScript
* @return Command to create the binding
*/
protected Command<Void> doAddJsBinding(String scriptName);
/**
* Remove JavaScript binding (internal)
* @param scriptName - Name of the binding to remove
* @return Command to remove the binding
*/
protected Command<Void> doRemoveJsBinding(String scriptName);
/**
* Get event for when JavaScript bindings are called (internal)
* @return Event handler for binding calls
*/
protected Event<BindingCalled> bindingCalledEvent();
/**
* Extract payload from binding event (internal)
* @param event - Binding called event
* @return Payload string from JavaScript
*/
protected String extractPayload(BindingCalled event);Manage script injection and pinning for new document creation.
/**
* Pin a script to be executed on every new document with a binding
* @param exposeScriptAs - Name to expose the script binding as
* @param script - JavaScript code to inject and pin
* @return ScriptId for the pinned script
*/
public ScriptId pin(String exposeScriptAs, String script);
/**
* Disable JavaScript functionality and clean up pinned scripts
*/
public void disable();
/**
* Add script to evaluate on new document creation (internal)
* @param script - JavaScript code to inject
* @return Command returning script identifier
*/
protected Command<ScriptIdentifier> addScriptToEvaluateOnNewDocument(String script);
/**
* Remove previously injected script (internal)
* @param id - Script identifier to remove
* @return Command to remove the script
*/
protected Command<Void> removeScriptToEvaluateOnNewDocument(ScriptIdentifier id);/**
* Identifier for injected scripts from CDP
*/
public class ScriptIdentifier {
// Generated from CDP protocol - exact structure depends on protocol version
}
/**
* Wrapper for pinned script management
*/
public class ScriptId {
public ScriptId(Object actualId);
public Object getActualId();
}/**
* Event data when JavaScript binding is called
*/
public class BindingCalled {
/**
* Get the payload sent from JavaScript
* @return Payload string
*/
public String getPayload();
/**
* Get the name of the binding that was called
* @return Binding name
*/
public String getName();
/**
* Get execution context ID where binding was called
* @return Context ID
*/
public ExecutionContextId getExecutionContextId();
}import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v99.V99Domains;
DevTools devTools = ...; // from ChromeDriver
V99Domains domains = new V99Domains(devTools);
// Add JavaScript binding using public API
domains.javascript().addJsBinding("myCallback");
// Listen for binding calls using public API
domains.javascript().addBindingCalledListener(payload -> {
System.out.println("Received from JavaScript: " + payload);
// Process the payload and potentially respond
processJavaScriptMessage(payload);
});
// In browser JavaScript, call: myCallback('Hello from browser!');// Pin a script to run on every new document
String initScript = """
console.log('Page initialized');
window.myApp = {
version: '1.0.0',
initialized: true
};
""";
// Pin script with exposed binding
ScriptId scriptId = domains.javascript().pin("initApp", initScript);
// The script will now run on every new page load
// and "initApp" binding will be available in JavaScript
// Cleanup when done
domains.javascript().disable(); // Removes all pinned scripts and bindings// Enable runtime for JavaScript operations
devTools.send(domains.javascript().enableRuntime());
// Perform JavaScript operations...
// (binding setup, script injection, etc.)
// Cleanup - disable runtime when done
devTools.send(domains.javascript().disableRuntime());
devTools.send(domains.javascript().disablePage());// Set up multiple bindings for bidirectional communication
domains.javascript().addJsBinding("sendToJava");
domains.javascript().addJsBinding("getFromJava");
// Single listener handles all binding calls
domains.javascript().addBindingCalledListener(payload -> {
// Parse payload to determine which binding was called
// The payload contains the binding name and data
if (payload.startsWith("sendToJava:")) {
String data = payload.substring("sendToJava:".length());
handleDataFromBrowser(data);
} else if (payload.startsWith("getFromJava:")) {
String request = payload.substring("getFromJava:".length());
String response = getDataForBrowser(request);
sendResponseToBrowser(response);
}
});
// In JavaScript:
// sendToJava('sendToJava:' + JSON.stringify(data));
// getFromJava('getFromJava:' + requestType);JavaScript operations can fail for several reasons:
Handle errors through standard DevTools exception handling and event monitoring:
try {
devTools.send(domains.javascript().enableRuntime());
devTools.send(domains.javascript().doAddJsBinding("myBinding"));
} catch (DevToolsException e) {
System.err.println("Failed to set up JavaScript binding: " + e.getMessage());
}
// Monitor for JavaScript exceptions
devTools.addListener(domains.events().exceptionThrownEvent(), exception -> {
System.err.println("JavaScript exception: " + exception.getExceptionDetails().getText());
});Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v99