Skip to content

[py] Prevent TypeError and KeyError in ErrorHandler when value is None or missing - #18239

Merged
cgoldberg merged 1 commit into
SeleniumHQ:trunkfrom
saiyam-jain-dev:fix-errorhandler-none-or-missing-value
Oct 25, 2026
Merged

cgoldberg merged 1 commit into
SeleniumHQ:trunkfrom
saiyam-jain-dev:fix-errorhandler-none-or-missing-value

Conversation

@saiyam-jain-dev

@saiyam-jain-dev saiyam-jain-dev commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

When a WebDriver server returns an error response where "value" is None or omitted from the response dictionary:

  1. If "value" was missing, response["value"] raised KeyError: 'value'.
  2. If "value" was None: if message == "" and "message" in value raised TypeError: argument of type 'NoneType' is not iterable.

In both cases, ErrorHandler.check_response() crashed with an unexpected internal exception instead of translating the status code into the appropriate WebDriverException subclass (e.g. NoSuchElementException, TimeoutException, WebDriverException).

This PR:

  • Uses response.get("value") instead of response["value"].
  • Guards dict-specific field extraction ("message" in value, "screen" in value, value.get("stackTrace"), alert data, etc.) with isinstance(value, dict).
  • Adds unit tests covering value=None, missing value, and numeric status codes without a payload.

Motivation and Context

Prevents unhandled KeyError and TypeError when WebDriver or intermediary proxy/gateway returns an error response without a payload or with a null value.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@CLAassistant

CLAassistant commented Oct 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@selenium-ci selenium-ci added the C-py Python Bindings label Oct 25, 2026
…e or missing

When WebDriver server responds with an error where 'value' is None or
omitted entirely from the response dictionary, ErrorHandler crashed with
KeyError ('value') or TypeError (argument of type 'NoneType' is not iterable)
instead of raising the appropriate WebDriverException subclass.

Safely extract value with response.get('value') and guard dictionary
operations with isinstance(value, dict).
@saiyam-jain-dev
saiyam-jain-dev force-pushed the fix-errorhandler-none-or-missing-value branch from cba6edc to 44fe882 Compare October 25, 2026 06:48
@cgoldberg
cgoldberg self-requested a review October 25, 2026 14:38

@cgoldberg cgoldberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
I'll merge this after CI runs.
Thanks for your first contribution @saiyam-jain-dev!

@saiyam-jain-dev

Copy link
Copy Markdown
Contributor Author

Thank you @cgoldberg! Glad to contribute to Selenium.

@cgoldberg
cgoldberg merged commit 652351e into SeleniumHQ:trunk Oct 25, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-py Python Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants