SeleniumHQ/selenium · error · WebDriverException
The command's generator function did not exit when expected!
Error message
The command's generator function did not exit when expected!
What it means
Internal assertion in _deserialize_result. BiDi command objects are generator-based: _serialize_command does next(command) for the request, and _deserialize_result does command.send(result) expecting the generator to terminate (StopIteration) and return its parsed value. If the generator yields again instead of returning, this error fires - meaning the command implementation is structurally wrong.
Source
Thrown at py/selenium/webdriver/remote/websocket_connection.py:174
return id(_callback)
on = add_callback
def remove_callback(self, event, callback_id):
event_name = event.event_class
if event_name in self.callbacks:
for callback in self.callbacks[event_name]:
if id(callback) == callback_id:
self.callbacks[event_name].remove(callback)
return
def _serialize_command(self, command):
return next(command)
def _deserialize_result(self, result, command):
try:
_ = command.send(result)
raise WebDriverException("The command's generator function did not exit when expected!")
except StopIteration as exit:
return exit.value
def _start_ws(self):
def on_open(ws):
self._started = True
def on_message(ws, message):
self._process_message(message)
def on_error(ws, error):
logger.debug(f"error: {error}")
ws.close()
def run_socket():
if self.url.startswith("wss://"):
self._ws.run_forever(sslopt={"cert_reqs": CERT_NONE}, suppress_origin=True)
else:View on GitHub (pinned to aa36b38e69)
Solutions
- Report upstream - this indicates a defect in the command implementation, not your inputs.
- If you wrote a custom command, ensure its generator yields exactly once then returns the parsed result.
- Pin/upgrade the selenium version where the command works correctly.
Example fix
# custom command generator - before (yields twice)
# def cmd():
# payload = yield {...}
# extra = yield {...} # wrong
# return parse(payload)
# after (yield once, then return)
def cmd():
payload = yield {"method": "x", "params": {}}
return parse(payload) Defensive patterns
Strategy: try-catch
Try / catch
try:
result = conn.execute(cmd)
except WebDriverException as e:
if "generator function did not exit" in str(e):
log.error("Defective BiDi command implementation: %r", cmd)
raise Prevention
- Do not hand-author BiDi command generators unless following the single-yield contract.
- Pin a known-good selenium release when relying on BiDi commands.
When it happens
Trigger: A BiDi command class whose generator has more than one yield, or a custom/monkeypatched command that does not `return` after consuming the result. Not triggered by ordinary inputs.
Common situations: Almost exclusively a Selenium-internal defect, or a user who subclassed/extended the BiDi command generators incorrectly.
Related errors
- interval must be a positive number
- Pattern must be an instance of UrlPattern. Received: '${patt
- Pattern must be an instance of UrlPattern. Received:'${patte
- Pattern must be an instance of String. Received:'${pattern}'
- Pass in a CaptureScreenshotParameters object. Received: ${ca
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/5e3bb8d6ac7c7146.
Report an issue: GitHub.