SeleniumHQ/selenium · error · BrowserError
-32700
-32700
Error message
Client received invalid JSON
What it means
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.
Source
Thrown at py/private/cdp.py:466
Dispatches responses to commands and events to listeners.
"""
global devtools
if devtools is None:
raise RuntimeError("CDP devtools module not loaded. Call import_devtools() first.")
while True:
try:
message = await self.ws.get_message()
except WsConnectionClosed:
# If the WebSocket is closed, we don't want to throw an
# exception from the reader task. Instead we will throw
# exceptions from the public API methods, and we can quietly
# exit the reader task here.
break
try:
data = json.loads(message)
except json.JSONDecodeError:
raise BrowserError(
{
"code": -32700,
"message": "Client received invalid JSON",
"data": message,
}
)
logger.debug("Received message %r", data)
if "sessionId" in data:
session_id = devtools.target.SessionID(data["sessionId"])
try:
session = self.sessions[session_id]
except KeyError:
raise BrowserError(
{
"code": -32700,
"message": "Browser sent a message for an invalid session",
"data": f"{session_id!r}",
}View on GitHub (pinned to aa36b38e69)
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.
Example fix
# before
async with conn:
doc = await dom.get_document() # BrowserError surfaces from reader
# after
try:
async with conn:
doc = await dom.get_document()
except BrowserError as e:
if e.code == -32700:
logger.error('Invalid JSON from browser: %s', e.data)
# reconnect or report
raise Defensive patterns
Strategy: try-catch
Try / catch
from selenium.webdriver.common.bidi.cdp import BrowserError
try:
async with conn:
await dom.get_document()
except BrowserError as e:
if e.code == -32700 and 'invalid JSON' in str(e):
logger.error('Malformed WebSocket frame: %s', e.data)
# reconnect or abort
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- {fn_name}() must be called in a connection context.
- {fn_name}() must be called in a session context.
- CDP devtools module not loaded. Call import_devtools() first
- Unable to find url to connect to from capabilities
- interval must be a positive number
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/d0b4ebff5ca8faac.
Report an issue: GitHub.