Element class represents an HTML DOM element and provides methods for interaction, property access, and manipulation.
Understanding elements
Elements in nodriver wrap CDP (Chrome DevTools Protocol) DOM nodes and provide a Pythonic interface for:- Clicking and user interaction
- Getting and setting properties
- Querying child elements
- Executing JavaScript on elements
- Taking screenshots
Elements are created by tab methods like
find(), select(), and query_selector(). You typically don’t instantiate them directly.Getting elements
From tab searches
From other elements
Query within an element’s children:Element properties
Core properties
Access DOM node properties:HTML attributes
Access element attributes directly:The
class attribute is accessed as class_ (with underscore) to avoid conflicting with Python’s class keyword.Text content
Children and parent
Navigate the DOM tree:Clicking and interaction
Click element
The standard click method:- Resolves the element’s remote object
- Flashes the element briefly (for debugging)
- Executes a JavaScript click on the element
- Triggers with user gesture
Mouse click
Click at the element’s position using mouse events:Which mouse button to use (‘left’, ‘right’, ‘middle’).
Bit field for pressed buttons (1=left, 2=right, 4=middle).
Bit field for modifier keys (Alt=1, Ctrl=2, Meta=4, Shift=8).
Mouse hover
Trigger hover effects without clicking:Drag and drop
Drag an element to another location:Target element or (x, y) coordinates.
Treat coordinates as relative offsets.
Number of intermediate points (higher = smoother but slower).
Form input
Send text
Type text into input fields:Sending special characters like
\n or \r\n can trigger form submissions. Useful when click doesn’t work!Clear input
Upload files
Send files to file input elements:Focus element
Select options
For<select> dropdown options:
JavaScript execution
Apply JavaScript function
Execute JavaScript with the element as a parameter:JavaScript function that receives the element as its parameter.
Return the actual value instead of a RemoteObject.
Call element methods
Shorthand for calling methods:Get JavaScript attributes
Retrieve all JavaScript properties:Position and visibility
Get position
Get the element’s position and dimensions:Scroll into view
Ensure the element is visible:Screenshots
Capture an image of just the element:Save path. When ‘auto’, generates a name from the page URL and timestamp.
Image format: ‘jpeg’ or ‘png’.
Scale factor (1=normal, 2=double size, 0.5=half size).
Debugging and highlighting
Flash element
Briefly show a red indicator on the element:DevTools-style highlight
Highlight element like Chrome DevTools:Content manipulation
Get HTML
Get the element’s outer HTML:Set value
For text nodes:Set text content
Remove from DOM
Delete the element:Save to DOM
Save modifications back to the DOM:Updating elements
Refresh element data and populate tree:- Fetches the latest DOM state
- Populates
parentandchildrenproperties - Resolves the remote object
- Updates attributes
Use
await element as shorthand for await element.update().Special element types
Iframe elements
Access iframe content:Shadow DOM
Access shadow root children:Video elements
Record video playback:Properties reference
Commonly used
Element tag name in lowercase (e.g., ‘div’, ‘button’).
Alias for
tag.CDP node ID.
Backend node ID used for many CDP operations.
Node type: 1=element, 3=text, 8=comment, 9=document.
Dictionary of element attributes.
Text content of this element only.
Concatenated text of element and all descendants.
Parent element.
Direct child elements.
The tab this element belongs to.
Best practices
Check element existence
Check element existence
Finding methods may return
None after timeout. Always check before using:Use click() for most interactions
Use click() for most interactions
The standard
click() method is more reliable than mouse_click() for most use cases.Update when DOM changes
Update when DOM changes
If the page updates, call
await element.update() to refresh element properties.Scroll into view before interaction
Scroll into view before interaction
Some elements need to be visible:
Use flash() for debugging
Use flash() for debugging
When developing, use
await element.flash() to verify you’ve found the right element.Related documentation
- Tab management - Finding elements from tabs
- Browser management - Browser lifecycle
- Configuration - Browser setup