Skip to content

Conversation

cgoldberg
Copy link
Contributor

@cgoldberg cgoldberg commented Sep 4, 2025

User description

💥 What does this PR do?

This PR makes find_element() and find_elements() methods on WebDriver, WebElement, and ShadowRoot classes raise an InvalidSelectorException("Compound class names are not allowed.") exception when called with a By.CLASS_NAME locator and a compound class name. A "compound class name" is considered any class name that contains whitespace. Having only surrounding whitespace won't trigger this exception because that will be rejected later by the webdriver.

The previous behavior was that it would attempt to find the element, but would always fail with NoSuchElementException and an unhelpful message.

This brings Python's behavior in line with other bindings that don't allow compound class names.

💡 Additional Considerations

This PR is better than the previous behavior, but I think a better solution would be to allow compound class names.

From a webdriver's perspective, there is really no such thing as finding an element by "class name". It's just a convenience that gets translated to a CSS selector before requesting it from the webdriver. So finding an element by class name foo really just tries to find it using CSS selector .foo.

In the case of compound class names, we could convert the whitespace to dots. If a user called these methods with By.CLASS_NAME values of a b c or a b c, instead of rejecting it, we could convert it to CSS selector .a.b.c. That would then allow compound class names to be used.

See #16292

Note: This PR also re-enables many element finding tests that were marked as excepted failures. The underlying issues should work correctly in newer browsers.

🔄 Types of changes

  • Bug fix
  • Breaking change (fix or feature that would cause existing functionality to change)

PR Type

Bug fix, Enhancement


Description

  • Add InvalidSelectorException for compound class names in find_element methods

  • Remove xfail markers from tests that now work correctly

  • Improve error handling for invalid selectors

  • Align Python behavior with other language bindings


Diagram Walkthrough

flowchart LR
  A["find_element/find_elements"] --> B["Check if By.CLASS_NAME"]
  B --> C["Check for whitespace in value"]
  C --> D["Raise InvalidSelectorException"]
  C --> E["Continue normal processing"]
Loading

File Walkthrough

Relevant files
Bug fix
webdriver.py
Add compound class name validation                                             

py/selenium/webdriver/remote/webdriver.py

  • Import InvalidSelectorException
  • Add compound class name validation in find_element method
  • Add compound class name validation in find_elements method
  • Check for whitespace in class name values and raise exception
+10/-0   
Tests
driver_element_finding_tests.py
Update tests for compound class name validation                   

py/test/selenium/webdriver/common/driver_element_finding_tests.py

  • Remove xfail markers for Chrome and Edge from multiple test methods
  • Update test expectation from NoSuchElementException to
    InvalidSelectorException
  • Clean up comment formatting for consistency
  • Enable previously disabled tests that now work correctly
+16/-56 

@selenium-ci selenium-ci added the C-py Python Bindings label Sep 4, 2025
Copy link
Contributor

qodo-merge-pro bot commented Sep 4, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified

@SeleniumHQ SeleniumHQ deleted a comment from qodo-merge-pro bot Sep 4, 2025
@cgoldberg cgoldberg merged commit 0ff768a into SeleniumHQ:trunk Sep 12, 2025
4 checks passed
@cgoldberg cgoldberg deleted the py-compound-class-names-invalidselector branch September 12, 2025 12:07
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.

2 participants