Skip to content

Conversation

@pixelsapphire
Copy link
Contributor

@pixelsapphire pixelsapphire commented Dec 21, 2025

User description

🔗 Related Issues

#15697

💥 What does this PR do?

It just adds type hints for WebElement.find_element(s) value argument

🔧 Implementation Notes

Because WebDriver.find_element(s) is typed the same way, and adding type hints here makes type checkers happy

💡 Additional Considerations

  • Yes, probably docs

🔄 Types of changes

  • Cleanup (formatting, renaming)

PR Type

Enhancement


Description

  • Add type hints for value parameter in find_element and find_elements

  • Specify value argument as str | None for consistency with WebDriver

  • Improve type checker compatibility for WebElement locator methods


Diagram Walkthrough

flowchart LR
  A["WebElement.find_element<br/>and find_elements"] -- "Add type hint<br/>value: str | None" --> B["Improved type<br/>checking support"]
Loading

File Walkthrough

Relevant files
Enhancement
webelement.py
Add type hints to find_element(s) value parameter               

py/selenium/webdriver/remote/webelement.py

  • Add type hint str | None to value parameter in find_element method
  • Add type hint str | None to value parameter in find_elements method
  • Align type hints with existing WebDriver.find_element(s)
    implementation
+2/-2     

@CLAassistant
Copy link

CLAassistant commented Dec 21, 2025

CLA assistant check
All committers have signed the CLA.

@selenium-ci selenium-ci added the C-py Python Bindings label Dec 21, 2025
@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Dec 21, 2025

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🟡
🎫 #5678
🔴 Investigate and fix repeated ChromeDriver instantiation causing "Error: ConnectFailure
(Connection refused)" after the first instance.
Provide guidance or a workaround for the reported Chrome/ChromeDriver/Selenium versions on
Ubuntu 16.04.
🟡
🎫 #1234
🔴 Restore/ensure that clicking a link with JavaScript in its href triggers the JavaScript as
it did in Selenium 2.47.1 (regression in 2.48.x) on Firefox 42.
Validate the behavior using the provided test case/videos and Firefox environment
described.
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

  • Update
Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Dec 21, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Add validation for None value

In the find_element method, add a check to raise a ValueError if the value
parameter is None to prevent unexpected behavior and improve error handling.

py/selenium/webdriver/remote/webelement.py [510-533]

 def find_element(self, by=By.ID, value: str | None = None) -> WebElement:
     """Find an element given a By strategy and locator.
 
     Args:
         by: The locating strategy to use. Default is `By.ID`. Supported values include:
             `By.ID`, `By.XPATH`, `By.LINK_TEXT`, `By.PARTIAL_LINK_TEXT`, `By.NAME`,
             `By.TAG_NAME`, `By.CLASS_NAME`, `By.CSS_SELECTOR`
         value: The value to use for the locator.
 
     Returns:
         The `WebElement` object that was found.
 
     Raises:
         NoSuchElementException: if the element wasn't found.
     """
+    if value is None:
+        raise ValueError("Locator value cannot be None")
     by, value = self._parent.locator_converter.convert(by, value)
     return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})["value"]
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies that passing value=None to find_element is a valid but likely unintended use case that can lead to subtle bugs. Adding an explicit check improves the robustness and clarity of the API, which is a valuable improvement.

Medium
  • Update

@cgoldberg cgoldberg changed the title Add type hints for WebElement.find_element(s) value argument [py] Add type hints for WebElement.find_element(s) Dec 21, 2025
Copy link
Member

@cgoldberg cgoldberg left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@cgoldberg cgoldberg left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@cgoldberg cgoldberg merged commit 02abb3b into SeleniumHQ:trunk Dec 23, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants