Chrome DevTools Protocol version 129 bindings for Selenium WebDriver.
—
The v129Javascript class provides comprehensive JavaScript execution and binding capabilities through the Chrome DevTools Protocol. It enables script injection, JavaScript binding management, and code execution in browser contexts.
Core JavaScript domain for script execution and binding management.
/**
* JavaScript domain for script execution and binding management
* @param devtools - DevTools instance for protocol communication
*/
public class v129Javascript extends Javascript<ScriptIdentifier, BindingCalled> {
public v129Javascript(DevTools devtools);
}Control the Runtime domain for JavaScript execution capabilities.
/**
* Enables the Runtime domain for JavaScript execution
* @return Command to enable Runtime domain
*/
protected Command<Void> enableRuntime();
/**
* Disables the Runtime domain
* @return Command to disable Runtime domain
*/
protected Command<Void> disableRuntime();Manage JavaScript bindings between browser and Java code.
/**
* Adds a JavaScript binding with the specified name
* @param scriptName - Name of the binding to add
* @return Command to add JavaScript binding
*/
protected Command<Void> doAddJsBinding(String scriptName);
/**
* Removes a JavaScript binding with the specified name
* @param scriptName - Name of the binding to remove
* @return Command to remove JavaScript binding
*/
protected Command<Void> doRemoveJsBinding(String scriptName);
/**
* Event fired when a JavaScript binding is called
* @return Event for binding calls
*/
protected Event<BindingCalled> bindingCalledEvent();
/**
* Extracts payload from binding called event
* @param event - BindingCalled event
* @return Payload string from the binding call
*/
protected String extractPayload(BindingCalled event);Control the Page domain for document-level script management.
/**
* Enables the Page domain for document script management
* @return Command to enable Page domain
*/
protected Command<Void> enablePage();
/**
* Disables the Page domain
* @return Command to disable Page domain
*/
protected Command<Void> disablePage();Manage scripts that execute automatically on new documents.
/**
* Adds script to evaluate on every new document
* @param script - JavaScript code to execute on new documents
* @return Command returning ScriptIdentifier for the added script
*/
protected Command<ScriptIdentifier> addScriptToEvaluateOnNewDocument(String script);
/**
* Removes script from evaluation on new documents
* @param id - ScriptIdentifier of the script to remove
* @return Command to remove the script
*/
protected Command<Void> removeScriptToEvaluateOnNewDocument(ScriptIdentifier id);Usage Examples:
import org.openqa.selenium.devtools.v129.v129Javascript;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v129.page.model.ScriptIdentifier;
// Create JavaScript domain
DevTools devTools = driver.getDevTools();
v129Javascript javascript = new v129Javascript(devTools);
// Enable domains
devTools.send(javascript.enableRuntime());
devTools.send(javascript.enablePage());
// Add JavaScript binding
devTools.send(javascript.doAddJsBinding("myBinding"));
// Listen for binding calls
devTools.addListener(javascript.bindingCalledEvent(), bindingEvent -> {
String payload = javascript.extractPayload(bindingEvent);
System.out.println("Binding called with payload: " + payload);
// Process the payload and potentially respond
if (payload.contains("getData")) {
// Handle data request from JavaScript
}
});
// Add script to execute on new documents
String initScript = """
window.myBinding = function(data) {
// Call Java binding
window.binding('myBinding', JSON.stringify(data));
};
console.log('Initialization script loaded');
""";
ScriptIdentifier scriptId = devTools.send(
javascript.addScriptToEvaluateOnNewDocument(initScript)
);
// Navigate to trigger script execution
driver.get("https://example.com");
// Execute JavaScript that uses the binding
driver.executeScript("""
window.myBinding({
action: 'getData',
timestamp: Date.now()
});
""");// Dynamic script injection based on page conditions
String conditionalScript = """
if (window.location.hostname === 'specific-site.com') {
// Site-specific initialization
window.customAPI = {
sendData: function(data) {
window.binding('dataBinding', JSON.stringify(data));
}
};
}
""";
devTools.send(javascript.addScriptToEvaluateOnNewDocument(conditionalScript));
// Multiple bindings for different purposes
devTools.send(javascript.doAddJsBinding("dataBinding"));
devTools.send(javascript.doAddJsBinding("errorBinding"));
devTools.send(javascript.doAddJsBinding("logBinding"));
// Handle different binding types
devTools.addListener(javascript.bindingCalledEvent(), bindingEvent -> {
String payload = javascript.extractPayload(bindingEvent);
try {
JsonObject data = JsonParser.parseString(payload).getAsJsonObject();
String bindingType = data.get("binding").getAsString();
switch (bindingType) {
case "dataBinding":
handleDataBinding(data);
break;
case "errorBinding":
handleErrorBinding(data);
break;
case "logBinding":
handleLogBinding(data);
break;
}
} catch (Exception e) {
System.err.println("Error processing binding: " + e.getMessage());
}
});
// Cleanup scripts when done
devTools.send(javascript.removeScriptToEvaluateOnNewDocument(scriptId));
devTools.send(javascript.doRemoveJsBinding("myBinding"));// From v129.page.model package
import org.openqa.selenium.devtools.v129.page.model.ScriptIdentifier;
// From v129.runtime.model package
import org.openqa.selenium.devtools.v129.runtime.model.BindingCalled;// ScriptIdentifier for managing injected scripts
class ScriptIdentifier {
public ScriptIdentifier(String value);
public String getValue();
public String toString();
}// BindingCalled event structure
class BindingCalled {
public String getName(); // Binding name
public String getPayload(); // Data payload
public Optional<Integer> getExecutionContextId();
}// Base JavaScript interface from idealized package
import org.openqa.selenium.devtools.idealized.Javascript;
// Command and Event base types
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;// For advanced JavaScript integration
import org.openqa.selenium.devtools.v129.runtime.Runtime;
import org.openqa.selenium.devtools.v129.page.Page;
// Optional parameters for enhanced functionality
import java.util.Optional;Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v129