[py] Raise InvalidSelectorException for compound class names #16291
+30
−56
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.
User description
💥 What does this PR do?
This PR makes
find_element()
andfind_elements()
methods onWebDriver
,WebElement
, andShadowRoot
classes raise anInvalidSelectorException("Compound class names are not allowed.")
exception when called with aBy.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 ofa b c
ora 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
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
File Walkthrough
webdriver.py
Add compound class name validation
py/selenium/webdriver/remote/webdriver.py
driver_element_finding_tests.py
Update tests for compound class name validation
py/test/selenium/webdriver/common/driver_element_finding_tests.py
InvalidSelectorException