Skip to main content
The Tab class represents a browser tab (or target) and provides the primary interface for interacting with web pages. Each tab maintains its own connection to the Chrome DevTools Protocol.

Understanding tabs

In nodriver, a “tab” can represent:
  • A browser tab (the most common case)
  • A browser window
  • An iframe
  • A service worker or background process
When you navigate to a different page in the same tab, the Tab object remains the same. Keep references to your tab objects for continued interaction.

Getting a tab

From the browser

Access existing tabs

The get() method handles navigation with built-in wait logic:
The get() method automatically waits for DOM events and handles timing, making it the safest way to navigate.

History navigation

Finding elements

nodriver provides multiple powerful methods for locating elements on the page.

Find by text

Find elements containing specific text:
str
required
The text to search for. Script contents are also considered text.
bool
default:"True"
When True, returns the element with the most similar text length. This helps find the “Login” button instead of scripts containing “login”.
float
default:"10"
Seconds to wait for the element to appear before returning None.

Find by CSS selector

Use standard CSS selectors:
str
required
CSS selector string, e.g., a[href], button[class*=close], a > img[src]

Find by XPath

str
required
XPath expression to evaluate.
float
default:"2.5"
Seconds to retry finding elements before returning an empty list.

Wait for elements

Block until an element appears:
If the element doesn’t appear within the timeout period, wait_for() raises asyncio.TimeoutError.

Page interaction

Scrolling

JavaScript execution

Execute JavaScript in the page context:
str
required
JavaScript expression to evaluate.
bool
default:"False"
Wait for the expression to resolve if it returns a Promise.
bool
default:"False"
Return the actual value instead of a RemoteObject reference.

Dump JavaScript objects

Serialize JavaScript objects to Python dictionaries:

Window management

Get window bounds

Resize and position

Activate tab

Bring the tab to the foreground:

Page content

Get page source

Take screenshots

Debugging

Open DevTools inspector

Open the Chrome DevTools for this tab in your default browser:
Get the inspector URL:

Waiting and timing

Sleep

Pause execution and allow the browser to update:
Using await tab or await tab.sleep() allows the script to “breathe” and ensures references are up to date. This is crucial when pages are loading or changing.

Using await on tabs

Closing tabs

Close the current tab:

Custom CDP commands

Send raw Chrome DevTools Protocol commands:
The cdp package contains all Chrome DevTools Protocol domains, methods, events, and types. Each CDP method returns a generator that you pass to tab.send().

Advanced features

Query selector methods

Lower-level element finding (used internally by find and select):

Mouse control

Flash point

Display a visual indicator at coordinates (useful for debugging):

Properties

Access tab properties:

Best practices

  • Use find() with best_match=True for user-visible text
  • Use select() for known element structures
  • Use xpath() for complex queries or case-insensitive searches
Tab operations are asynchronous. Missing await is a common source of bugs.
When encountering timing issues or elements not found, add await tab or await tab.sleep() to allow page rendering to complete.
Finding methods return None after timeout. Check return values before using elements.