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

Method _reader_task

py/private/cdp.py:444–491  ·  view source on GitHub ↗

Runs in the background and handles incoming messages. Dispatches responses to commands and events to listeners.

(self)

Source from the content-addressed store, hash-verified

442 return session
443
444 async def _reader_task(self):
445 """Runs in the background and handles incoming messages.
446
447 Dispatches responses to commands and events to listeners.
448 """
449 global devtools
450 if devtools is None:
451 raise RuntimeError("CDP devtools module not loaded. Call import_devtools() first.")
452 while True:
453 try:
454 message = await self.ws.get_message()
455 except WsConnectionClosed:
456 # If the WebSocket is closed, we don't want to throw an
457 # exception from the reader task. Instead we will throw
458 # exceptions from the public API methods, and we can quietly
459 # exit the reader task here.
460 break
461 try:
462 data = json.loads(message)
463 except json.JSONDecodeError as err:
464 raise BrowserError(
465 {
466 "code": -32700,
467 "message": "Client received invalid JSON",
468 "data": message,
469 }
470 ) from err
471 logger.debug("Received message %r", data)
472 if "sessionId" in data:
473 session_id = devtools.target.SessionID(data["sessionId"])
474 try:
475 session = self.sessions[session_id]
476 except KeyError as err:
477 raise BrowserError(
478 {
479 "code": -32700,
480 "message": "Browser sent a message for an invalid session",
481 "data": f"{session_id!r}",
482 }
483 ) from err
484 session._handle_data(data)
485 else:
486 self._handle_data(data)
487
488 for session in self.sessions.values():
489 for senders in session.channels.values():
490 for sender in senders:
491 sender.close()
492
493
494@asynccontextmanager

Callers

nothing calls this directly

Calls 5

BrowserErrorClass · 0.85
_handle_dataMethod · 0.80
valuesMethod · 0.80
closeMethod · 0.65
debugMethod · 0.45

Tested by

no test coverage detected