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

Method add_handler

py/private/_script_handlers.py:456–491  ·  view source on GitHub ↗

Register a mutation handler for the given mutation types.

(self, callback: Callable, mutation_types: str | Iterable[str] | None = None)

Source from the content-addressed store, hash-verified

454 )
455
456 def add_handler(self, callback: Callable, mutation_types: str | Iterable[str] | None = None) -> int:
457 """Register a mutation handler for the given mutation types."""
458 types = self._normalize_types(mutation_types)
459
460 def _dispatch(message: Any) -> None:
461 if not isinstance(message, dict) or message.get("channel") != self._channel:
462 return
463 data = message.get("data")
464 value = data.get("value") if isinstance(data, dict) else None
465 if value is None:
466 return
467 try:
468 payload = json.loads(value)
469 except (ValueError, TypeError):
470 return
471 if not isinstance(payload, dict):
472 return
473 mutation = dom_mutation_from_payload(payload)
474 if mutation.type == "attributes" and not mutation.element_id and mutation.element_id != "0":
475 return
476 if mutation.type in types:
477 callback(mutation)
478
479 conn = self._script._conn
480 with self._lock:
481 channel_arg = self._channel_argument()
482 missing = set(types) - self._active_types
483 if missing:
484 self._observe_types(channel_arg, missing)
485 self._active_types |= missing
486 if not self._handlers:
487 self._subscription_id = _subscribe_to_event(conn, self.EVENT)
488 # Register the callback AFTER setup to avoid leaking it if setup fails.
489 callback_id = conn.add_callback(_EventRef(self.EVENT), _dispatch)
490 self._handlers[callback_id] = types
491 return callback_id
492
493 def remove_handler(self, callback_id: int) -> None:
494 """Remove a handler; tears down observers with the last one."""

Callers

nothing calls this directly

Calls 6

_normalize_typesMethod · 0.95
_channel_argumentMethod · 0.95
_observe_typesMethod · 0.95
_subscribe_to_eventFunction · 0.85
_EventRefClass · 0.85
add_callbackMethod · 0.45

Tested by

no test coverage detected