{"record":{"id":"c52f246570a82558","repo":"SeleniumHQ/selenium","slug":"self-label-capitalize-handler-id-not-foun","errorCode":null,"errorMessage":"{self._label.capitalize()} '{handler_id}' not found","messagePattern":"(.+?) '(.+?)' not found","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"py/private/_network_handlers.py","lineNumber":715,"sourceCode":"        if isinstance(url_patterns, str):\n            url_patterns = [url_patterns]\n        patterns = list(url_patterns) if url_patterns else None\n        bidi_patterns = globs_to_url_patterns(patterns)\n        intercept_result = self._network._add_intercept(phases=[self._phase], url_patterns=bidi_patterns)\n        intercept_id = intercept_result.get(\"intercept\") if intercept_result else None\n        if self._subscription_callback_id is None:\n            self._subscription_callback_id = self._network.add_event_handler(self._event_name, self._on_event)\n        self._counter += 1\n        handler_id = f\"{self._id_prefix}-{self._counter}\"\n        self._handlers[handler_id] = _HandlerEntry(handler_id, patterns, callback, intercept_id)\n        logger.debug(\"Added %s %s (patterns=%s)\", self._label, handler_id, patterns)\n        return handler_id\n\n    def remove_handler(self, handler_id: str) -> None:\n        \"\"\"Remove a handler and its intercept by handler ID.\"\"\"\n        entry = self._handlers.pop(handler_id, None)\n        if entry is None:\n            raise ValueError(f\"{self._label.capitalize()} '{handler_id}' not found\")\n        if entry.intercept_id:\n            self._network._remove_intercept(entry.intercept_id)\n        if not self._keep_subscription() and self._subscription_callback_id is not None:\n            self._network.remove_event_handler(self._event_name, self._subscription_callback_id)\n            self._subscription_callback_id = None\n        logger.debug(\"Removed %s %s\", self._label, handler_id)\n\n    def clear(self) -> None:\n        \"\"\"Remove all registered handlers and their intercepts.\"\"\"\n        for handler_id in list(self._handlers):\n            self.remove_handler(handler_id)\n\n    def intercept_ids(self) -> set:\n        \"\"\"Intercept IDs owned by this registry's handlers.\"\"\"\n        return {entry.intercept_id for entry in self._handlers.values() if entry.intercept_id}\n\n    def _keep_subscription(self) -> bool:\n        \"\"\"Whether the event subscription is still needed.\"\"\"","sourceCodeStart":697,"sourceCodeEnd":733,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/private/_network_handlers.py#L697-L733","documentation":"Raised as a ValueError by _HandlerRegistry.remove_handler() when the handler_id argument is not present in the internal _handlers dictionary. Each add_handler() call returns a unique handler_id string (formatted as '{prefix}-{counter}'); remove_handler pops from _handlers and throws if the key does not exist. The _label attribute (e.g. 'request handler', 'response handler') is capitalized into the message to identify which handler type was not found.","triggerScenarios":"Calling remove_handler(handler_id) with an ID that was never returned by add_handler, was already removed in a prior call, or was invalidated by a clear() call. The _handlers.pop(handler_id, None) returns None, triggering the raise.","commonSituations":"Calling remove_handler twice with the same ID (double-removal); storing the handler_id in a variable that gets overwritten or lost; calling remove_handler after clear() which already removed all handlers; a typo or wrong variable passed as handler_id.","solutions":["Store the handler_id returned by add_handler() and pass exactly that value to remove_handler(); never reuse an ID after removal.","Before removing, check membership: `if handler_id in registry._handlers` — or better, track removed IDs in your own set.","Use clear() instead of individual remove_handler() calls if you want to remove all handlers at once.","Guard against double-removal by setting the stored handler_id to None after a successful removal."],"exampleFix":"# before\nhandler_id = registry.add_handler(['*'], callback)\nregistry.remove_handler(handler_id)\nregistry.remove_handler(handler_id)  # ValueError\n\n# after\nhandler_id = registry.add_handler(['*'], callback)\nregistry.remove_handler(handler_id)\nhandler_id = None  # prevent double-removal\nif handler_id is not None:\n    registry.remove_handler(handler_id)","handlingStrategy":"validation","validationCode":"# Track handler IDs and check before removing\nactive_handlers = set()\nhid = registry.add_handler(['*'], callback)\nactive_handlers.add(hid)\nif hid in active_handlers:\n    registry.remove_handler(hid)\n    active_handlers.discard(hid)","typeGuard":null,"tryCatchPattern":"try:\n    registry.remove_handler(handler_id)\nexcept ValueError as e:\n    if 'not found' in str(e):\n        # already removed or never added; safe to ignore\n        pass\n    else:\n        raise","preventionTips":["Store handler IDs in a set and discard after removal to prevent double-removal.","Use clear() to remove all handlers at once when appropriate.","Null out handler ID variables after removal."],"tags":["python","bidi","network","handlers","validation"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}