Overview
The Element class represents an HTML DOM element. You typically obtain Element instances through Tab methods likeselect(), find(), or query_selector().
Creating elements
Elements are not created directly. Instead, they’re returned from Tab methods:Properties
tag
The HTML tag name in lowercase.tag_name
Alias fortag.
node_id
The CDP node ID.backend_node_id
The CDP backend node ID.node_type
The node type (1 = element, 3 = text, etc.).node_name
The node name in uppercase.text
The text content of this element.text_all
All text content including children.attrs
Element attributes as a ContraDict.parent
The parent element.children
List of child elements.tab
The parent Tab instance.Interaction methods
click()
Click the element.mouse_click()
Native mouse click on the element.Mouse button:
"left", "right", or "middle".Which button (1 = left, 2 = right, 4 = middle).
Modifier keys (Alt=1, Ctrl=2, Meta=4, Shift=8).
Hold the mouse button down.
mouse_move()
Move the mouse to the element position.mouse_drag()
Drag the element to a destination.Target element or coordinates (x, y).
Treat coordinates as relative offsets.
Number of intermediate points (higher = smoother).
send_keys()
Send text or key events to the element.Text string, special key, or list of key events.
send_file()
Send file(s) to a file input element.One or more file paths to upload.
focus()
Focus the element.clear_input()
Clear an input field by setting its value to empty string.clear_input_by_deleting()
Clear input by simulating delete key presses.select_option()
Select this option element (for<option> tags in <select>).
scroll_into_view()
Scroll the element into view.Query methods
query_selector()
Find a descendant element by CSS selector.CSS selector.
The found element, or None.
query_selector_all()
Find all descendant elements by CSS selector.CSS selector.
List of matching elements.
JavaScript methods
apply()
Apply a JavaScript function to the element.JavaScript function that accepts the element as a parameter.Examples:
"(elem) => elem.value = 'text'""elem => elem.play()""function myFunc(elem) { alert(elem) }"
Return the value directly.
Wait for promises to resolve.
The result of the JavaScript function.
get_js_attributes()
Get all JavaScript properties of the element.Dictionary of element properties.
Attribute methods
get()
Get an attribute value by name.Attribute name (e.g.,
"href", "src", "class").The attribute value, or None if not found.
set_value()
Set the node value.The value to set.
set_text()
Set the text content.The text to set.
get_html()
Get the outer HTML of the element.The element’s HTML.
DOM manipulation
update()
Update the element to retrieve latest properties.The updated element.
save_to_dom()
Save element changes to the DOM.remove_from_dom()
Remove the element from the DOM.Position and visualization
get_position()
Get the element’s position on the page.Return absolute position including scroll offset.
Position object with x, y, width, height, center, or None if not visible.
flash()
Briefly display a red dot on the element.Duration in seconds.
highlight_overlay()
Highlight the element DevTools-style.Screenshots
screenshot_b64()
Capture a screenshot of this element as base64.Image format:
"jpeg" or "png".Scale factor (1 = original size, 2 = double, 0.5 = half).
Base64-encoded image data.
save_screenshot()
Save a screenshot of this element to a file.Save path. If “auto”, generates from URL.
Image format:
"jpeg" or "png".Scale factor.
Path to saved screenshot.
Special features
record_video()
Record video from an HTML5<video> element.
Desired filename.
Download folder path.
Record for this many seconds then download.
<video> elements. Recording stops and downloads when the video ends, pauses, or stops.
is_recording()
Check if a video element is currently recording.True if recording.
Examples
Basic interaction
Form filling
JavaScript execution
Position and visibility
Screenshots
Drag and drop
Working with children
Notes
- Always use
awaitwith Element methods - Call
await element.update()to refresh the element if the DOM changed - For text nodes (node_type == 3), access
.parentto get the containing element - Screenshots only work for visible elements
- Use
get()method to access attributes safely