DOM Commands
DOM commands provide comprehensive functionality for interacting with the Document Object Model of web pages.
Overview
The DOM commands module is one of the most important modules in Pydoll, providing all the functionality needed to find, interact with, and manipulate HTML elements on web pages.
pydoll.commands.dom_commands
DomCommands
Implementation of Chrome DevTools Protocol for the DOM domain.
This class provides commands for interacting with the Document Object Model (DOM) in the browser, enabling access and manipulation of the element structure in a web page. The DOM domain in Chrome DevTools Protocol exposes operations for reading and writing to the DOM, which is fundamental for browser automation, testing, and debugging.
Each DOM element is represented by a mirror object with a unique ID. This ID can be used to gather additional information about the node, resolve it into JavaScript object wrappers, manipulate attributes, and perform various other operations on the DOM structure.
describe_node
staticmethod
Describes a DOM node identified by its ID without requiring domain to be enabled.
The describe_node command is particularly useful in scenarios where you need to quickly gather information about a specific element without subscribing to DOM change events, making it more lightweight for isolated element inspection operations.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Identifier of the node known to the client.
TYPE:
|
backend_node_id
|
Identifier of the backend node used internally by the browser.
TYPE:
|
object_id
|
JavaScript object id of the node wrapper.
TYPE:
|
depth
|
Maximum depth at which children should be retrieved (default is 1). Use -1 for the entire subtree or provide an integer greater than 0.
TYPE:
|
pierce
|
Whether iframes and shadow roots should be traversed when returning the subtree (default is false).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns detailed information about the requested node.
TYPE:
|
disable
staticmethod
Disables DOM agent for the current page.
Disabling the DOM domain stops the CDP from sending DOM-related events and prevents further DOM manipulation operations until the domain is enabled again. This can be important for optimizing performance when you're done with DOM operations and want to minimize background processing.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to disable the DOM domain.
TYPE:
|
enable
staticmethod
Enables DOM agent for the current page.
Enabling the DOM domain is a prerequisite for receiving DOM events and using most DOM manipulation methods. The DOM events include changes to the DOM tree structure, attribute modifications, and many others. Without enabling this domain first, many DOM operations would fail or provide incomplete information.
PARAMETER | DESCRIPTION |
---|---|
include_whitespace
|
Whether to include whitespace-only text nodes in the children array of returned Nodes. Allowed values: "none", "all".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to enable the DOM domain.
TYPE:
|
focus
staticmethod
Focuses the given element.
The focus command is crucial for simulating realistic user interactions, as many events (like keyboard input) require that an element has focus first. It's also important for testing proper tab order and keyboard accessibility of web pages.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Identifier of the node to focus.
TYPE:
|
backend_node_id
|
Identifier of the backend node to focus.
TYPE:
|
object_id
|
JavaScript object id of the node wrapper.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to focus on the specified element.
TYPE:
|
get_attributes
staticmethod
Returns attributes for the specified node.
Attribute information is essential in web testing and automation because attributes often contain crucial information about element state, behavior, and metadata. This command provides an efficient way to access all attributes of an element without parsing HTML or using JavaScript evaluation.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to retrieve attributes for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns an interleaved array of node attribute names and values [name1, value1, name2, value2, ...].
TYPE:
|
get_box_model
staticmethod
Returns box model information for the specified node.
The box model is a fundamental concept in CSS that describes how elements are rendered with content, padding, borders, and margins. This command provides detailed information about these dimensions and coordinates, which is invaluable for spatial analysis and precision interactions with elements on the page.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Identifier of the node.
TYPE:
|
backend_node_id
|
Identifier of the backend node.
TYPE:
|
object_id
|
JavaScript object id of the node wrapper.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the box model for the node, including coordinates for content, padding, border, and margin boxes.
TYPE:
|
get_document
staticmethod
Returns the root DOM node (and optionally the subtree) to the caller.
This is typically the first command called when interacting with the DOM, as it provides access to the document's root node. From this root, you can traverse to any other element on the page. This command implicitly enables DOM domain events for the current target, making it a good starting point for DOM interaction.
PARAMETER | DESCRIPTION |
---|---|
depth
|
Maximum depth at which children should be retrieved (default is 1). Use -1 for the entire subtree or provide an integer greater than 0.
TYPE:
|
pierce
|
Whether iframes and shadow roots should be traversed when returning the subtree (default is false).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the root DOM node.
TYPE:
|
get_node_for_location
staticmethod
Returns node id at given location on the page.
This command is particularly useful for bridging the gap between visual/pixel-based information and the DOM structure. It allows you to convert screen coordinates to actual DOM elements, which is essential for creating inspection tools or for testing spatially-oriented interactions.
PARAMETER | DESCRIPTION |
---|---|
x
|
X coordinate relative to the main frame's viewport.
TYPE:
|
y
|
Y coordinate relative to the main frame's viewport.
TYPE:
|
include_user_agent_shadow_dom
|
Whether to include nodes in user agent shadow roots.
TYPE:
|
ignore_pointer_events_none
|
Whether to ignore pointer-events:none and test elements underneath them.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the node at the given location, including frame information when available.
TYPE:
|
get_outer_html
staticmethod
Returns node's HTML markup, including the node itself and all its children.
This command provides a way to access the complete HTML representation of an element, making it valuable for when you need to extract, analyze, or verify HTML content. It's more comprehensive than just getting text content as it preserves the full markup structure including tags, attributes, and child elements.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Identifier of the node.
TYPE:
|
backend_node_id
|
Identifier of the backend node.
TYPE:
|
object_id
|
JavaScript object id of the node wrapper.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the outer HTML markup of the node.
TYPE:
|
hide_highlight
staticmethod
Hides any DOM element highlight.
This command is particularly useful in automation workflows where multiple elements are highlighted in sequence, and you need to clear previous highlights before proceeding to the next element to avoid visual clutter or interference.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to hide DOM element highlights.
TYPE:
|
highlight_node
staticmethod
Highlights DOM node.
Highlighting nodes is especially valuable during development and debugging sessions to visually confirm which elements are being selected by selectors or coordinates.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to highlight a DOM node.
TYPE:
|
highlight_rect
staticmethod
Highlights given rectangle.
Unlike node highlighting, rectangle highlighting allows highlighting arbitrary regions of the page, which is useful for highlighting computed areas or regions that don't correspond directly to DOM elements.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to highlight a rectangular area.
TYPE:
|
move_to
staticmethod
Moves node into the new container, placing it before the given anchor.
This command allows for more complex DOM restructuring than simple attribute or content changes. It's particularly useful when testing applications that involve rearranging elements, such as sortable lists, kanban boards, or drag-and-drop interfaces.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to move.
TYPE:
|
target_node_id
|
Id of the element to drop the moved node into.
TYPE:
|
insert_before_node_id
|
Drop node before this one (if absent, the moved node becomes the last child of target_node_id).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to move a node, returning the new id of the moved node.
TYPE:
|
query_selector
staticmethod
Executes querySelector on a given node.
This method is one of the most fundamental tools for element location, allowing the use of standard CSS selectors to find elements in the DOM. Unlike JavaScript's querySelector, this can be executed on any node (not just document), enabling scoped searches within specific sections of the page.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to query upon.
TYPE:
|
selector
|
CSS selector string.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the first element matching the selector.
TYPE:
|
query_selector_all
staticmethod
Executes querySelectorAll on a given node.
This method extends querySelector by returning all matching elements rather than just the first one. This is essential for operations that need to process multiple elements, such as extracting data from tables, lists, or grids, or verifying that the correct number of elements are present.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to query upon.
TYPE:
|
selector
|
CSS selector string.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns all elements matching the selector.
TYPE:
|
remove_attribute
staticmethod
Removes attribute with given name from an element with given id.
This command allows direct manipulation of element attributes without using JavaScript in the page context. It's useful for testing how elements behave when specific attributes are removed or for preparing elements for specific test conditions.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the element to remove attribute from.
TYPE:
|
name
|
Name of the attribute to remove.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to remove the specified attribute.
TYPE:
|
remove_node
staticmethod
Removes node with given id.
This command allows direct removal of DOM elements, which can be useful when testing how an application responds to missing elements or when simplifying a page for focused testing scenarios.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to remove.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to remove the specified node.
TYPE:
|
request_child_nodes
staticmethod
Requests that children of the node with given id are returned to the caller.
This method is particularly useful when dealing with large DOM trees, as it allows for more efficient exploration by loading children on demand rather than loading the entire tree at once. Child nodes are returned as setChildNodes events.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to get children for.
TYPE:
|
depth
|
The maximum depth at which children should be retrieved, defaults to 1. Use -1 for the entire subtree.
TYPE:
|
pierce
|
Whether or not iframes and shadow roots should be traversed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to request child nodes.
TYPE:
|
request_node
staticmethod
Requests that the node is sent to the caller given the JavaScript node object reference.
This method bridges the gap between JavaScript objects in the page context and the CDP's node representation system, allowing automation to work with elements that might only be available as JavaScript references (e.g., from event handlers).
PARAMETER | DESCRIPTION |
---|---|
object_id
|
JavaScript object id to convert into a Node.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the Node id for the given object.
TYPE:
|
resolve_node
staticmethod
Resolves the JavaScript node object for a given NodeId or BackendNodeId.
This method provides the opposite functionality of requestNode - instead of getting a CDP node from a JavaScript object, it gets a JavaScript object from a CDP node. This enables executing JavaScript operations on nodes identified through CDP.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to resolve.
TYPE:
|
backend_node_id
|
Backend id of the node to resolve.
TYPE:
|
object_group
|
Symbolic group name that can be used to release multiple objects.
TYPE:
|
execution_context_id
|
Execution context in which to resolve the node.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns a JavaScript object wrapper for the node.
TYPE:
|
scroll_into_view_if_needed
staticmethod
Scrolls the specified node into view if not already visible.
This command is crucial for reliable web automation, as it ensures elements are actually visible in the viewport before attempting interactions. Modern websites often use lazy loading and have long scrollable areas, making this command essential for working with elements that may not be initially visible.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Identifier of the node.
TYPE:
|
backend_node_id
|
Identifier of the backend node.
TYPE:
|
object_id
|
JavaScript object id of the node wrapper.
TYPE:
|
rect
|
Optional rect to scroll into view, relative to the node bounds.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to scroll the element into view.
TYPE:
|
set_attributes_as_text
staticmethod
Sets attribute for an element with given id, using text representation.
This command allows for more complex attribute manipulation than set_attribute_value, as it accepts a text representation that can potentially define multiple attributes or include special formatting. It's particularly useful when trying to replicate exactly how attributes would be defined in HTML source code.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the element to set attribute for.
TYPE:
|
text
|
Text with a new attribute value.
TYPE:
|
name
|
Attribute name to replace with new text value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to set an attribute as text.
TYPE:
|
set_attribute_value
staticmethod
Sets attribute for element with given id.
This command provides direct control over element attributes without using JavaScript, which is essential for testing how applications respond to attribute changes or for setting up specific test conditions by controlling element attributes directly.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the element to set attribute for.
TYPE:
|
name
|
Attribute name.
TYPE:
|
value
|
Attribute value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to set an attribute value.
TYPE:
|
set_file_input_files
staticmethod
Sets files for the given file input element.
This command solves one of the most challenging automation problems: working with file inputs. It bypasses the OS-level file dialog that normally appears when clicking a file input, allowing automated tests to provide files programmatically.
PARAMETER | DESCRIPTION |
---|---|
files
|
list of file paths to set.
TYPE:
|
node_id
|
Identifier of the node.
TYPE:
|
backend_node_id
|
Identifier of the backend node.
TYPE:
|
object_id
|
JavaScript object id of the node wrapper.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to set files for a file input element.
TYPE:
|
set_node_name
staticmethod
Sets node name for a node with given id.
This command allows changing the actual tag name of an element, which can be useful for testing how applications handle different types of elements or for testing the impact of semantic HTML choices on accessibility and behavior.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to set name for.
TYPE:
|
name
|
New node name.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the new node id after the name change.
TYPE:
|
set_node_value
staticmethod
Sets node value for a node with given id.
This command is particularly useful for updating the content of text nodes and comments, allowing direct manipulation of text content without changing the surrounding HTML structure.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to set value for.
TYPE:
|
value
|
New node value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to set a node's value.
TYPE:
|
set_outer_html
staticmethod
Sets node HTML markup, replacing existing one.
This is one of the most powerful DOM manipulation commands, as it allows completely replacing an element and all its children with new HTML. This is useful for making major structural changes to the page or for testing how applications handle dynamically inserted content.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to set outer HTML for.
TYPE:
|
outer_html
|
HTML markup to set.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to set the outer HTML of a node.
TYPE:
|
collect_class_names_from_subtree
staticmethod
Collects class names for the node with given id and all of its children.
This method is valuable for understanding the styling landscape of a page, especially in complex applications where multiple CSS frameworks might be in use or where classes are dynamically applied.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to collect class names for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns a list of all unique class names in the subtree.
TYPE:
|
copy_to
staticmethod
Creates a deep copy of the specified node and places it into the target container.
Unlike move_to, this command creates a copy of the node, leaving the original intact. This is useful when you want to duplicate content rather than move it, such as when testing how multiple instances of the same component behave.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to copy.
TYPE:
|
target_node_id
|
Id of the element to drop the copy into.
TYPE:
|
insert_before_node_id
|
Drop the copy before this node (if absent, the copy becomes the last child of target_node_id).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the id of the new copy.
TYPE:
|
discard_search_results
staticmethod
Discards search results from the session with the given id.
This method helps manage resources when performing multiple searches during a session, allowing explicit cleanup of search results that are no longer needed.
PARAMETER | DESCRIPTION |
---|---|
search_id
|
Unique search session identifier.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to discard search results.
TYPE:
|
get_anchor_element
staticmethod
Finds the closest ancestor node that is an anchor element for the given node.
This method is useful when working with content inside links or when you need to find the enclosing link element for text or other elements. This helps in cases where you might locate text but need to find the actual link around it.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to search for an anchor around.
TYPE:
|
anchor_specifier
|
Optional specifier for anchor tag properties.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the anchor element node information.
TYPE:
|
get_container_for_node
staticmethod
get_container_for_node(node_id, container_name=None, physical_axes=None, logical_axes=None, queries_scroll_state=None)
Finds a containing element for the given node based on specified parameters.
This method helps in understanding the structural and layout context of elements, particularly in complex layouts using CSS features like flexbox, grid, or when dealing with scrollable containers.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to find the container for.
TYPE:
|
container_name
|
Name of the container to look for (e.g., 'scrollable', 'flex').
TYPE:
|
physical_axes
|
Physical axes to consider (Horizontal, Vertical, Both).
TYPE:
|
logical_axes
|
Logical axes to consider (Inline, Block, Both).
TYPE:
|
queries_scroll_state
|
Whether to query scroll state or not.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns information about the containing element.
TYPE:
|
get_content_quads
staticmethod
Returns quads that describe node position on the page.
This method provides detailed geometric information about an element's position on the page, accounting for any transformations, rotations, or other CSS effects. This is more precise than getBoxModel for complex layouts.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Identifier of the node.
TYPE:
|
backend_node_id
|
Identifier of the backend node.
TYPE:
|
object_id
|
JavaScript object id of the node wrapper.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the quads describing the node position.
TYPE:
|
get_detached_dom_nodes
staticmethod
Returns information about detached DOM tree elements.
This method is primarily useful for debugging memory issues related to the DOM, as detached DOM nodes (nodes no longer in the document but still referenced in JavaScript) are a common cause of memory leaks in web applications.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns information about detached DOM nodes.
TYPE:
|
get_element_by_relation
staticmethod
Retrieves an element related to the given one in a specified way.
This method provides a way to find elements based on their relationships to other elements, such as finding the next focusable element after a given one. This is useful for simulating keyboard navigation or for analyzing element relationships.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the reference node.
TYPE:
|
relation
|
Type of relationship (e.g., nextSibling, previousSibling, firstChild).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the related element node.
TYPE:
|
get_file_info
staticmethod
Returns file information for the given File object.
This method is useful when working with file inputs and the File API, providing access to file metadata like name, size, and MIME type for files selected in file input elements or created programmatically.
PARAMETER | DESCRIPTION |
---|---|
object_id
|
JavaScript object id of the File object to get info for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns file information.
TYPE:
|
get_frame_owner
staticmethod
Returns iframe element that owns the given frame.
This method is essential when working with pages that contain iframes, as it allows mapping between frame IDs (used in CDP) and the actual iframe elements in the parent document.
PARAMETER | DESCRIPTION |
---|---|
frame_id
|
Id of the frame to get the owner element for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the frame owner element.
TYPE:
|
get_nodes_for_subtree_by_style
staticmethod
Finds nodes with a given computed style in a subtree.
This method allows finding elements based on their computed styles rather than just structure or attributes. This is powerful for testing visual aspects of a page or for finding elements that match specific visual criteria.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Node to start the search from.
TYPE:
|
computed_styles
|
list of computed style properties to match against.
TYPE:
|
pierce
|
Whether or not iframes and shadow roots should be traversed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns nodes matching the specified styles.
TYPE:
|
get_node_stack_traces
staticmethod
Gets stack traces associated with a specific node.
This method is powerful for debugging, as it reveals the JavaScript execution paths that led to the creation of specific DOM elements, helping developers understand the relationship between their code and the resulting DOM structure.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to get stack traces for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns stack traces related to the node.
TYPE:
|
get_querying_descendants_for_container
staticmethod
Returns the querying descendants for container.
This method is particularly useful for working with CSS Container Queries, helping to identify which descendant elements are affected by or querying a particular container element.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the container node to find querying descendants for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns querying descendant information.
TYPE:
|
get_relayout_boundary
staticmethod
Returns the root of the relayout boundary for the given node.
This method helps in understanding layout performance by identifying the boundary of layout recalculations when a particular element changes. This is valuable for optimizing rendering performance.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
Id of the node to find relayout boundary for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the relayout boundary node.
TYPE:
|
get_search_results
staticmethod
Returns search results from given fromIndex
to given toIndex
from a search.
This method is used in conjunction with performSearch to retrieve search results in batches, which is essential when dealing with large result sets that might be inefficient to transfer all at once.
PARAMETER | DESCRIPTION |
---|---|
search_id
|
Unique search session identifier from performSearch.
TYPE:
|
from_index
|
Start index to retrieve results from.
TYPE:
|
to_index
|
End index to retrieve results to (exclusive).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the requested search results.
TYPE:
|
get_top_layer_elements
staticmethod
Returns all top layer elements in the document.
This method is valuable for working with modern web UIs that make extensive use of overlays, modals, dropdowns, and other elements that need to appear above the normal document flow.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the top layer element information.
TYPE:
|
mark_undoable_state
staticmethod
Marks last undoable state.
This method helps in managing DOM manipulation state, allowing the creation of savepoints that can be reverted to with the undo command. This is useful for complex sequences of DOM operations that should be treated as a unit.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to mark the current state as undoable.
TYPE:
|
perform_search
staticmethod
Searches for a given string in the DOM tree.
This method initiates a search across the DOM tree, supporting plain text, CSS selectors, or XPath expressions. It's a powerful way to find elements or content across the entire document without knowing the exact structure.
PARAMETER | DESCRIPTION |
---|---|
query
|
Plain text or query selector or XPath search query.
TYPE:
|
include_user_agent_shadow_dom
|
True to include user agent shadow DOM in the search.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns search results identifier and count.
TYPE:
|
push_node_by_path_to_frontend
staticmethod
Requests that the node is sent to the caller given its path.
This method provides an alternative way to reference nodes when node IDs aren't available, using path expressions instead. This can be useful when integrating with systems that identify elements by path rather than by ID.
PARAMETER | DESCRIPTION |
---|---|
path
|
Path to node in the proprietary format.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns the node id for the node.
TYPE:
|
push_nodes_by_backend_ids_to_frontend
staticmethod
Requests that a batch of nodes is sent to the caller given their backend node ids.
This method allows for efficient batch processing when you have multiple backend node IDs and need to convert them to frontend node IDs for further operations.
PARAMETER | DESCRIPTION |
---|---|
backend_node_ids
|
The array of backend node ids.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command that returns an array of node ids.
TYPE:
|
redo
staticmethod
Re-does the last undone action.
This method works in conjunction with undo and markUndoableState to provide a transactional approach to DOM manipulations, allowing for stepping back and forth through a sequence of changes.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to redo the last undone action.
TYPE:
|
set_inspected_node
staticmethod
Enables console to refer to the node with given id via $x command line API.
This method creates a bridge between automated testing/scripting and manual console interaction, making it easy to reference specific nodes in the console for debugging or experimentation.
PARAMETER | DESCRIPTION |
---|---|
node_id
|
DOM node id to be accessible by means of $x command line API.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to set the inspected node.
TYPE:
|
set_node_stack_traces_enabled
staticmethod
Sets if stack traces should be captured for Nodes.
This method enables or disables the collection of stack traces when DOM nodes are created, which can be extremely valuable for debugging complex applications to understand where and why specific DOM elements are being created.
PARAMETER | DESCRIPTION |
---|---|
enable
|
Enable or disable stack trace collection.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to enable or disable node stack traces.
TYPE:
|
undo
staticmethod
Undoes the last performed action.
This method works in conjunction with redo and markUndoableState to provide transactional control over DOM manipulations, allowing for reverting changes when needed.
RETURNS | DESCRIPTION |
---|---|
Command
|
CDP command to undo the last performed action.
TYPE:
|
Usage
DOM commands are used extensively by the WebElement
class and element finding methods:
from pydoll.commands.dom_commands import query_selector, get_attributes
from pydoll.connection.connection_handler import ConnectionHandler
# Find element and get its attributes
connection = ConnectionHandler()
node_id = await query_selector(connection, selector="#username")
attributes = await get_attributes(connection, node_id=node_id)
Key Functionality
The DOM commands module provides functions for:
Element Finding
query_selector()
- Find single element by CSS selectorquery_selector_all()
- Find multiple elements by CSS selectorget_document()
- Get the document root node
Element Interaction
click_element()
- Click on elementsfocus_element()
- Focus elementsset_attribute_value()
- Set element attributesget_attributes()
- Get element attributes
Element Information
get_box_model()
- Get element positioning and dimensionsdescribe_node()
- Get detailed element informationget_outer_html()
- Get element HTML content
DOM Manipulation
remove_node()
- Remove elements from DOMset_node_value()
- Set element valuesrequest_child_nodes()
- Get child elements
High-Level APIs
While these commands provide powerful low-level access, most users should use the higher-level WebElement
class methods like click()
, type_text()
, and get_attribute()
which use these commands internally.