MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / add_handler

Method add_handler

py/private/_script_handlers.py:451–485  ·  view source on GitHub ↗

Register a handler and subscribe to ``log.entryAdded`` if needed. Args: callback: User callback invoked with the shaped payload. category: ``console`` or ``error``. legacy: When ``True`` the callback receives the generated ``ConsoleLogEntr

(self, callback: Callable, category: str, legacy: bool = False)

Source from the content-addressed store, hash-verified

449 self._categories: dict[int, str] = {}
450
451 def add_handler(self, callback: Callable, category: str, legacy: bool = False) -> int:
452 """Register a handler and subscribe to ``log.entryAdded`` if needed.
453
454 Args:
455 callback: User callback invoked with the shaped payload.
456 category: ``console`` or ``error``.
457 legacy: When ``True`` the callback receives the generated
458 ``ConsoleLogEntry`` / ``JavascriptLogEntry`` dataclasses;
459 otherwise it receives :class:`ConsoleMessage` /
460 :class:`ScriptError`.
461 """
462 entry_type = self._CATEGORY_ENTRY_TYPES[category]
463
464 def _dispatch(params: Any) -> None:
465 if not isinstance(params, dict) or params.get("type") != entry_type:
466 return
467 if legacy:
468 payload = _legacy_log_entry(params)
469 elif category == self.CONSOLE:
470 payload = console_message_from_log_entry(params)
471 else:
472 payload = script_error_from_log_entry(params)
473 callback(payload)
474
475 conn = self._script._conn
476 with self._lock:
477 callback_id = conn.add_callback(_EventRef(self.EVENT), _dispatch)
478 if not self._categories:
479 try:
480 self._subscription_id = _subscribe_to_event(conn, self.EVENT)
481 except Exception:
482 conn.remove_callback(_EventRef(self.EVENT), callback_id)
483 raise
484 self._categories[callback_id] = category
485 return callback_id
486
487 def remove_handler(self, callback_id: int) -> None:
488 """Remove a handler; drops the session subscription with the last one."""

Callers

nothing calls this directly

Calls 4

_EventRefClass · 0.85
_subscribe_to_eventFunction · 0.85
add_callbackMethod · 0.45
remove_callbackMethod · 0.45

Tested by

no test coverage detected