You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python: Accept By in find_element/find_elements type hints
🐞 Bug fix🕐 Less than 10 minutes
AI Description
• Restore type-checker compatibility for By.ID passed to find_element(s).
• Widen locator argument unions across WebDriver, WebElement, and ShadowRoot.
• Preserve compatibility with custom/Appium string strategies and RelativeBy locators.
Diagram
graph TD
A["Client code"] --> B["WebDriver.find_element(s)"] --> F["Remote command execute"]
A --> C["WebElement.find_element(s)"] --> D["Locator converter"] --> F
A --> E["ShadowRoot.find_element(s)"] --> F
G["Locator arg (str | By | RelativeBy)"] --> B
Loading
High-Level Assessment
The following are alternative approaches to this PR:
1. Introduce a shared `ByArg` TypeAlias
➕ Avoids repeating unions across modules (str | By | RelativeBy).
➕ Makes future locator-type expansions a single-line change.
➖ Requires choosing an import location (public vs internal typing module).
➖ Slightly larger refactor surface than a direct signature tweak.
2. Use overloads for different locator families
➕ Can express more precise typing per locator strategy (e.g., RelativeBy vs By vs str).
➕ Improves IDE help for common call patterns.
➖ More verbose and harder to maintain across multiple entry points.
➖ Risk of diverging overload sets between WebDriver/WebElement/ShadowRoot.
Recommendation: The chosen approach (widening the locator union to include By) is the most pragmatic fix: it restores static-type compatibility without changing runtime behavior or constraining Appium/custom string strategies. If follow-up cleanup is desired, consider a shared ByArg TypeAlias to reduce duplication, but it’s not required for correctness.
Files changed (3) +6 / -6
Bug fix (3) +6 / -6
shadowroot.pyAllow 'By' in ShadowRoot 'find_element(s)' type hints+2/-2
Allow 'By' in ShadowRoot 'find_element(s)' type hints
• Widen the 'by' parameter type from 'str' to 'str | By' for both 'find_element' and 'find_elements'. This restores type-checker acceptance of passing 'By.ID' (and other 'By' members) directly.
webdriver.pyAccept 'By' alongside 'RelativeBy' in WebDriver 'find_element(s)'+2/-2
Accept 'By' alongside 'RelativeBy' in WebDriver 'find_element(s)'
• Extend the 'by' parameter union from 'str | RelativeBy' to 'str | By | RelativeBy' for 'find_element' and 'find_elements'. This keeps RelativeBy support while restoring compatibility for 'By.*' locators under static type checking.
webelement.pyAllow 'By' in WebElement 'find_element(s)' type hints+2/-2
Allow 'By' in WebElement 'find_element(s)' type hints
• Widen the 'by' parameter type from 'str' to 'str | By' for both 'find_element' and 'find_elements'. This aligns WebElement typing with common usage ('By.ID', etc.) while preserving string-based custom strategies.
1. Misleading By type hint✗ Dismissed🐞 Bug⚙ Maintainability
Description
find_element/find_elements now annotate by as including By, but in this codebase By is a
namespace class whose locator constants are ByType string literals, not By instances. This
widens typing to allow By() instances to type-check even though they are forwarded as the
WebDriver payload using field and can fail JSON serialization at request time.
By is defined as a plain class holding string-literal constants (ByType), so including By in
the locator unions does not model real locator values. The WebDriver code forwards by directly
into the command params as using, and RemoteConnection.execute JSON-serializes those params;
therefore, allowing By instances via typing can lead to runtime JSON serialization failures.
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
## Issue description
The locator argument type hints were widened to include `By` (e.g., `by: str | By` / `by: str | By | RelativeBy`), but `By` in this repo is a namespace container of string constants (`ByType`), not the runtime type of locator values. This makes invalid inputs like `By()` appear type-safe while they will be forwarded as the wire-protocol `using` value.
## Issue Context
`find_element`/`find_elements` ultimately send `{ "using": by, "value": value }` through `RemoteConnection.execute`, which JSON-encodes params. Non-string objects (e.g., `By()` instances) are not JSON serializable.
## Fix Focus Areas
- py/selenium/webdriver/remote/webdriver.py[887-943]
- py/selenium/webdriver/remote/webelement.py[510-556]
- py/selenium/webdriver/remote/shadowroot.py[55-123]
### Suggested change
- Import and use `ByType` in annotations instead of `By`:
- `ShadowRoot`/`WebElement`: `by: str | ByType = By.ID`
- `WebDriver`: `by: str | ByType | RelativeBy = By.ID`
- Keep `str` in the union to support custom/Appium strategies, and keep `RelativeBy` where applicable.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 Related Issues
💥 What does this PR do?
find_element/find_elementslocator arguments🔧 Implementation Notes
Byto the accepted union sostr,ByType, custom/Appium strategies, andByall keep type-checking.ByTypewould re-break the AppiumByastrsubclass would forceBy.IDto one static type and break whichever ofby: By/by: ByTypeit didn't match.🤖 AI assistance
🔄 Types of changes