Runtime Commands
Runtime commands provide JavaScript execution capabilities and runtime environment management.
Overview
The runtime commands module enables JavaScript code execution, object inspection, and runtime environment control within browser contexts.
pydoll.commands.runtime_commands
RuntimeCommands
A class for interacting with the JavaScript runtime using Chrome DevTools Protocol.
This class provides methods to create commands for evaluating JavaScript expressions, calling functions on JavaScript objects, and retrieving object properties through CDP.
ATTRIBUTE | DESCRIPTION |
---|---|
EVALUATE_TEMPLATE |
Template for the Runtime.evaluate command.
TYPE:
|
CALL_FUNCTION_ON_TEMPLATE |
Template for the Runtime.callFunctionOn command.
TYPE:
|
GET_PROPERTIES |
Template for the Runtime.getProperties command.
TYPE:
|
add_binding
staticmethod
Creates a command to add a JavaScript binding.
PARAMETER | DESCRIPTION |
---|---|
name
|
Name of the binding to add.
TYPE:
|
execution_context_name
|
Name of the execution context to bind to.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to add a JavaScript binding. |
await_promise
staticmethod
Creates a command to await a JavaScript promise and return its result.
PARAMETER | DESCRIPTION |
---|---|
promise_object_id
|
ID of the promise to await.
TYPE:
|
return_by_value
|
Whether to return the result by value instead of reference.
TYPE:
|
generate_preview
|
Whether to generate a preview for the result.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[AwaitPromiseResponse]
|
Command[AwaitPromiseResponse]: Command object to await a promise. |
call_function_on
staticmethod
call_function_on(function_declaration, object_id=None, arguments=None, silent=None, return_by_value=None, generate_preview=None, user_gesture=None, await_promise=None, execution_context_id=None, object_group=None, throw_on_side_effect=None, unique_context_id=None, serialization_options=None)
Creates a command to call a function with a given declaration on a specific object.
PARAMETER | DESCRIPTION |
---|---|
function_declaration
|
Declaration of the function to call.
TYPE:
|
object_id
|
ID of the object to call the function on.
TYPE:
|
arguments
|
Arguments to pass to the function.
TYPE:
|
silent
|
Whether to silence exceptions.
TYPE:
|
return_by_value
|
Whether to return the result by value instead of reference.
TYPE:
|
generate_preview
|
Whether to generate a preview for the result.
TYPE:
|
user_gesture
|
Whether to treat the call as initiated by user gesture.
TYPE:
|
await_promise
|
Whether to await promise result.
TYPE:
|
execution_context_id
|
ID of the execution context to call the function in.
TYPE:
|
object_group
|
Symbolic group name for the result.
TYPE:
|
throw_on_side_effect
|
Whether to throw if side effect cannot be ruled out.
TYPE:
|
unique_context_id
|
Unique context ID for the function call.
TYPE:
|
serialization_options
|
Serialization options for the result.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[CallFunctionOnResponse]
|
Command[CallFunctionOnResponse]: Command object to call a function on an object. |
compile_script
staticmethod
Creates a command to compile a JavaScript expression.
PARAMETER | DESCRIPTION |
---|---|
expression
|
JavaScript expression to compile.
TYPE:
|
source_url
|
URL of the source file for the script.
TYPE:
|
persist_script
|
Whether to persist the compiled script.
TYPE:
|
execution_context_id
|
ID of the execution context to compile the script in.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[CompileScriptResponse]
|
Command[CompileScriptResponse]: Command object to compile a script. |
disable
staticmethod
Disables the runtime domain.
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to disable the runtime domain. |
enable
staticmethod
Enables the runtime domain.
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to enable the runtime domain. |
evaluate
staticmethod
evaluate(expression, object_group=None, include_command_line_api=None, silent=None, context_id=None, return_by_value=None, generate_preview=None, user_gesture=None, await_promise=None, throw_on_side_effect=None, timeout=None, disable_breaks=None, repl_mode=None, allow_unsafe_eval_blocked_by_csp=None, unique_context_id=None, serialization_options=None)
Creates a command to evaluate a JavaScript expression in the global context.
PARAMETER | DESCRIPTION |
---|---|
expression
|
JavaScript expression to evaluate.
TYPE:
|
object_group
|
Symbolic group name for the result.
TYPE:
|
include_command_line_api
|
Whether to include command line API.
TYPE:
|
silent
|
Whether to silence exceptions.
TYPE:
|
context_id
|
ID of the execution context to evaluate in.
TYPE:
|
return_by_value
|
Whether to return the result by value instead of reference.
TYPE:
|
generate_preview
|
Whether to generate a preview for the result.
TYPE:
|
user_gesture
|
Whether to treat evaluation as initiated by user gesture.
TYPE:
|
await_promise
|
Whether to await promise result.
TYPE:
|
throw_on_side_effect
|
Whether to throw if side effect cannot be ruled out.
TYPE:
|
timeout
|
Timeout in milliseconds.
TYPE:
|
disable_breaks
|
Whether to disable breakpoints during evaluation.
TYPE:
|
repl_mode
|
Whether to execute in REPL mode.
TYPE:
|
allow_unsafe_eval_blocked_by_csp
|
Allow unsafe evaluation.
TYPE:
|
unique_context_id
|
Unique context ID for evaluation.
TYPE:
|
serialization_options
|
Serialization for the result.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[EvaluateResponse]
|
Command[EvaluateResponse]: Command object to evaluate JavaScript. |
get_properties
staticmethod
get_properties(object_id, own_properties=None, accessor_properties_only=None, generate_preview=None, non_indexed_properties_only=None)
Creates a command to get properties of a JavaScript object.
PARAMETER | DESCRIPTION |
---|---|
object_id
|
ID of the object to get properties for.
TYPE:
|
own_properties
|
Whether to return only own properties.
TYPE:
|
accessor_properties_only
|
Whether to return only accessor properties.
TYPE:
|
generate_preview
|
Whether to generate previews for property values.
TYPE:
|
non_indexed_properties_only
|
Whether to return only non-indexed properties.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[GetPropertiesResponse]
|
Command[GetPropertiesResponse]: Command object to get object properties. |
global_lexical_scope_names
staticmethod
Creates a command to retrieve names of variables from global lexical scope.
PARAMETER | DESCRIPTION |
---|---|
execution_context_id
|
ID of the execution context to get scope names from.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[GlobalLexicalScopeNamesResponse]
|
Command[GlobalLexicalScopeNamesResponse]: Command object to get global lexical scope names. |
query_objects
staticmethod
Creates a command to query objects with a given prototype.
PARAMETER | DESCRIPTION |
---|---|
prototype_object_id
|
ID of the prototype object.
TYPE:
|
object_group
|
Symbolic group name for the results.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[QueryObjectsResponse]
|
Command[QueryObjectsResponse]: Command object to query objects. |
release_object
staticmethod
Creates a command to release a JavaScript object.
PARAMETER | DESCRIPTION |
---|---|
object_id
|
ID of the object to release.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to release an object. |
release_object_group
staticmethod
Creates a command to release all objects in a group.
PARAMETER | DESCRIPTION |
---|---|
object_group
|
Name of the object group to release.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to release an object group. |
remove_binding
staticmethod
Creates a command to remove a JavaScript binding.
PARAMETER | DESCRIPTION |
---|---|
name
|
Name of the binding to remove.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to remove a JavaScript binding. |
run_script
staticmethod
run_script(script_id, execution_context_id=None, object_group=None, silent=None, include_command_line_api=None, return_by_value=None, generate_preview=None, await_promise=None)
Creates a command to run a compiled script.
PARAMETER | DESCRIPTION |
---|---|
script_id
|
ID of the compiled script to run.
TYPE:
|
execution_context_id
|
ID of the execution context to run the script in.
TYPE:
|
object_group
|
Symbolic group name for the result.
TYPE:
|
silent
|
Whether to silence exceptions.
TYPE:
|
include_command_line_api
|
Whether to include command line API.
TYPE:
|
return_by_value
|
Whether to return the result by value instead of reference.
TYPE:
|
generate_preview
|
Whether to generate a preview for the result.
TYPE:
|
await_promise
|
Whether to await promise result.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[RunScriptResponse]
|
Command[RunScriptResponse]: Command object to run a script. |
set_async_call_stack_depth
staticmethod
Creates a command to set the async call stack depth.
PARAMETER | DESCRIPTION |
---|---|
max_depth
|
Maximum depth of async call stacks.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to set async call stack depth. |
set_custom_object_formatter_enabled
staticmethod
Creates a command to enable or disable custom object formatters.
PARAMETER | DESCRIPTION |
---|---|
enabled
|
Whether to enable custom object formatters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to enable/disable custom object formatters. |
set_max_call_stack_size_to_capture
staticmethod
Creates a command to set the maximum call stack size to capture.
PARAMETER | DESCRIPTION |
---|---|
size
|
Maximum call stack size to capture.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to set max call stack size. |
Usage
Runtime commands are used for JavaScript execution and runtime management:
from pydoll.commands.runtime_commands import evaluate, enable
from pydoll.connection.connection_handler import ConnectionHandler
# Enable runtime events
connection = ConnectionHandler()
await enable(connection)
# Execute JavaScript
result = await evaluate(
connection,
expression="document.title",
return_by_value=True
)
print(result.value) # Page title
Key Functionality
The runtime commands module provides functions for:
JavaScript Execution
evaluate()
- Execute JavaScript expressionscall_function_on()
- Call functions on objectscompile_script()
- Compile JavaScript for reuserun_script()
- Run compiled scripts
Object Management
get_properties()
- Get object propertiesrelease_object()
- Release object referencesrelease_object_group()
- Release object groups
Runtime Control
enable()
/disable()
- Enable/disable runtime eventsdiscard_console_entries()
- Clear console entriesset_custom_object_formatter_enabled()
- Enable custom formatters
Exception Handling
set_async_call_stack_depth()
- Set call stack depth- Exception capture and reporting
- Error object inspection
Advanced Usage
Complex JavaScript Execution
# Execute complex JavaScript with error handling
script = """
try {
const elements = document.querySelectorAll('.item');
return Array.from(elements).map(el => ({
text: el.textContent,
href: el.href
}));
} catch (error) {
return { error: error.message };
}
"""
result = await evaluate(
connection,
expression=script,
return_by_value=True,
await_promise=True
)
Object Inspection
# Get detailed object properties
properties = await get_properties(
connection,
object_id=object_id,
own_properties=True,
accessor_properties_only=False
)
for prop in properties:
print(f"{prop.name}: {prop.value}")
Console Integration
Runtime commands integrate with browser console: - Console messages and errors - Console API method calls - Custom console formatters
Performance Considerations
JavaScript execution through runtime commands can be slower than native browser execution. Use judiciously for complex operations.