Skip to main content
The 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:
Access to parent and children requires the element to have its tree populated. Call await element.update() if these properties are None.

Clicking and interaction

Click element

The standard click method:
This method:
  1. Resolves the element’s remote object
  2. Flashes the element briefly (for debugging)
  3. Executes a JavaScript click on the element
  4. Triggers with user gesture

Mouse click

Click at the element’s position using mouse events:
str
default:"'left'"
Which mouse button to use (‘left’, ‘right’, ‘middle’).
int
default:"1"
Bit field for pressed buttons (1=left, 2=right, 4=middle).
int
default:"0"
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:
Element | Tuple[int, int]
required
Target element or (x, y) coordinates.
bool
default:"False"
Treat coordinates as relative offsets.
int
default:"1"
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:
str
required
JavaScript function that receives the element as its parameter.
bool
default:"True"
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:
PathLike
default:"'auto'"
Save path. When ‘auto’, generates a name from the page URL and timestamp.
str
default:"'jpeg'"
Image format: ‘jpeg’ or ‘png’.
float
default:"1"
Scale factor (1=normal, 2=double size, 0.5=half size).
If the element is hidden, has no size, or is not in the viewport, save_screenshot() raises a RuntimeError.

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:
This:
  • Fetches the latest DOM state
  • Populates parent and children properties
  • 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

str
Element tag name in lowercase (e.g., ‘div’, ‘button’).
str
Alias for tag.
int
CDP node ID.
int
Backend node ID used for many CDP operations.
int
Node type: 1=element, 3=text, 8=comment, 9=document.
ContraDict
Dictionary of element attributes.
str
Text content of this element only.
str
Concatenated text of element and all descendants.
Element
Parent element.
List[Element]
Direct child elements.
Tab
The tab this element belongs to.

Best practices

Finding methods may return None after timeout. Always check before using:
The standard click() method is more reliable than mouse_click() for most use cases.
If the page updates, call await element.update() to refresh element properties.
Some elements need to be visible:
When developing, use await element.flash() to verify you’ve found the right element.