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
Navigation
Navigate to a URL
Theget() 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:The text to search for. Script contents are also considered text.
When True, returns the element with the most similar text length. This helps find the “Login” button instead of scripts containing “login”.
Seconds to wait for the element to appear before returning None.
Find by CSS selector
Use standard CSS selectors:CSS selector string, e.g.,
a[href], button[class*=close], a > img[src]Find by XPath
XPath expression to evaluate.
Seconds to retry finding elements before returning an empty list.
Wait for elements
Block until an element appears:Page interaction
Scrolling
JavaScript execution
Execute JavaScript in the page context:JavaScript expression to evaluate.
Wait for the expression to resolve if it returns a Promise.
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: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:Understanding CDP commands
Understanding CDP 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 byfind and select):
Mouse control
Flash point
Display a visual indicator at coordinates (useful for debugging):Properties
Access tab properties:Best practices
Use appropriate finding methods
Use appropriate finding methods
- Use
find()withbest_match=Truefor user-visible text - Use
select()for known element structures - Use
xpath()for complex queries or case-insensitive searches
Always await tab operations
Always await tab operations
Tab operations are asynchronous. Missing
await is a common source of bugs.Let the page breathe
Let the page breathe
When encountering timing issues or elements not found, add
await tab or await tab.sleep() to allow page rendering to complete.Handle timeouts gracefully
Handle timeouts gracefully
Finding methods return
None after timeout. Check return values before using elements.Related documentation
- Element interaction - Working with found elements
- Browser management - Managing the browser instance
- Configuration - Browser configuration options