Skip to main content

Overview

nodriver defines custom exception classes to handle errors from the Chrome DevTools Protocol and browser operations. Understanding these exceptions helps you write robust automation scripts.

Exception Classes

ProtocolException

Raised when a Chrome DevTools Protocol command fails or returns an error.
Attributes:
str
Human-readable error message.
int | None
Error code from the protocol, if available.
tuple
Original exception arguments.
Example:
Common Scenarios:
  • Invalid node IDs in DOM operations
  • Calling CDP methods before enabling the domain
  • Invalid CSS selectors
  • Network errors
  • JavaScript execution errors

SettingClassVarNotAllowedException

Raised when attempting to set class variables that should not be modified.
This is an internal exception you typically won’t encounter in normal usage.

Standard Python Exceptions

nodriver also raises standard Python exceptions in certain situations:

FileNotFoundError

Raised when:
  • Chrome/Chromium executable cannot be found
  • Specified browser executable path doesn’t exist
  • Extension files cannot be found
Example:

RuntimeError

Raised when:
  • Creating Browser/Tab objects outside an async context
  • Invalid browser state operations
Example:

ValueError

Raised when:
  • Invalid configuration arguments
  • Invalid parameter values
Example:

TimeoutError

Raised when:
  • Element finding operations timeout
  • Wait conditions not met within timeout period
Example:

Error Handling Patterns

Graceful degradation

Retry logic

Context manager for cleanup

Logging errors

Type-specific handling

Best Practices

Always handle ProtocolException when sending CDP commands, as they can fail for many reasons.
Use timeouts appropriately - Set reasonable timeouts for find/select operations based on your use case.
Log errors with context - Include relevant information like URLs, selectors, and operation details in error logs.
Don’t catch all exceptions - Be specific about which exceptions you handle to avoid hiding bugs.
Clean up resources - Always stop browsers in finally blocks or use context managers to prevent resource leaks.

Common Error Messages

See also