{"record":{"id":"d0b4ebff5ca8faac","repo":"SeleniumHQ/selenium","slug":"32700","errorCode":"-32700","errorMessage":"Client received invalid JSON","messagePattern":"Client received invalid JSON","errorType":"error_code","errorClass":"BrowserError","httpStatus":null,"severity":"error","filePath":"py/private/cdp.py","lineNumber":466,"sourceCode":"\n        Dispatches responses to commands and events to listeners.\n        \"\"\"\n        global devtools\n        if devtools is None:\n            raise RuntimeError(\"CDP devtools module not loaded. Call import_devtools() first.\")\n        while True:\n            try:\n                message = await self.ws.get_message()\n            except WsConnectionClosed:\n                # If the WebSocket is closed, we don't want to throw an\n                # exception from the reader task. Instead we will throw\n                # exceptions from the public API methods, and we can quietly\n                # exit the reader task here.\n                break\n            try:\n                data = json.loads(message)\n            except json.JSONDecodeError:\n                raise BrowserError(\n                    {\n                        \"code\": -32700,\n                        \"message\": \"Client received invalid JSON\",\n                        \"data\": message,\n                    }\n                )\n            logger.debug(\"Received message %r\", data)\n            if \"sessionId\" in data:\n                session_id = devtools.target.SessionID(data[\"sessionId\"])\n                try:\n                    session = self.sessions[session_id]\n                except KeyError:\n                    raise BrowserError(\n                        {\n                            \"code\": -32700,\n                            \"message\": \"Browser sent a message for an invalid session\",\n                            \"data\": f\"{session_id!r}\",\n                        }","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/private/cdp.py#L448-L484","documentation":"Raised as a BrowserError (code -32700, the JSON-RPC parse error code) by CdpConnection._reader_task() when a message received from the CDP WebSocket fails to parse as JSON. The reader calls json.loads(message); on JSONDecodeError it constructs a BrowserError with code -32700, the message 'Client received invalid JSON', and the raw message data. This indicates the browser or intermediary sent malformed data over the WebSocket.","triggerScenarios":"The WebSocket receives a message that is not valid JSON (e.g. truncated, binary, or protocol-violating text). json.loads raises JSONDecodeError, caught and re-raised as BrowserError. This happens in the background reader task and propagates to the caller.","commonSituations":"A buggy or non-conformant browser/devtools endpoint sending malformed frames; a proxy or intermediary corrupting WebSocket messages; a connection issue causing partial/truncated frames; using a devtools protocol version mismatch that sends unexpected message formats.","solutions":["Verify the browser and Selenium/CDP endpoint versions are compatible — a version mismatch can cause unexpected message formats.","If behind a proxy or load balancer, check WebSocket frame integrity and buffering settings.","Add error handling around the CDP connection to catch BrowserError and reconnect if the connection becomes corrupt.","Enable debug logging to capture the raw message data field for diagnosis."],"exampleFix":"# before\nasync with conn:\n    doc = await dom.get_document()  # BrowserError surfaces from reader\n\n# after\ntry:\n    async with conn:\n        doc = await dom.get_document()\nexcept BrowserError as e:\n    if e.code == -32700:\n        logger.error('Invalid JSON from browser: %s', e.data)\n        # reconnect or report\n        raise","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from selenium.webdriver.common.bidi.cdp import BrowserError\ntry:\n    async with conn:\n        await dom.get_document()\nexcept BrowserError as e:\n    if e.code == -32700 and 'invalid JSON' in str(e):\n        logger.error('Malformed WebSocket frame: %s', e.data)\n        # reconnect or abort\n    else:\n        raise","preventionTips":["Ensure browser and CDP endpoint versions are compatible.","Check WebSocket integrity if behind a proxy.","Catch BrowserError at the connection level and reconnect on protocol corruption."],"tags":["python","cdp","websocket","json","browser-error","jsonrpc"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}