Skip to content

Commit 6699f02

Browse files
committed
[py] Avoid AttributeError and add to WebElement finders
1 parent fbb3fb9 commit 6699f02

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

py/selenium/webdriver/remote/webdriver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ def find_element(self, by=By.ID, value: Optional[str] = None) -> WebElement:
918918
The first matching `WebElement` found on the page.
919919
"""
920920
if by == "class name":
921-
if " " in value.strip():
921+
if value and any(char.isspace() for char in value.strip()):
922922
raise InvalidSelectorException("Compound class names are not allowed.")
923923

924924
by, value = self.locator_converter.convert(by, value)
@@ -958,7 +958,7 @@ def find_elements(self, by=By.ID, value: Optional[str] = None) -> list[WebElemen
958958
list of `WebElements` matching locator strategy found on the page.
959959
"""
960960
if by == "class name":
961-
if " " in value.strip():
961+
if value and any(char.isspace() for char in value.strip()):
962962
raise InvalidSelectorException("Compound class names are not allowed.")
963963

964964
by, value = self.locator_converter.convert(by, value)

py/selenium/webdriver/remote/webelement.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from hashlib import md5 as md5_hash
2626
from io import BytesIO
2727

28-
from selenium.common.exceptions import JavascriptException, WebDriverException
28+
from selenium.common.exceptions import InvalidSelectorException, JavascriptException, WebDriverException
2929
from selenium.webdriver.common.by import By
3030
from selenium.webdriver.common.utils import keys_to_typing
3131

@@ -598,7 +598,12 @@ def find_element(self, by=By.ID, value=None) -> WebElement:
598598
WebElement
599599
The first matching `WebElement` found on the page.
600600
"""
601+
if by == "class name":
602+
if value and any(char.isspace() for char in value.strip()):
603+
raise InvalidSelectorException("Compound class names are not allowed.")
604+
601605
by, value = self._parent.locator_converter.convert(by, value)
606+
602607
return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})["value"]
603608

604609
def find_elements(self, by=By.ID, value=None) -> list[WebElement]:
@@ -627,7 +632,12 @@ def find_elements(self, by=By.ID, value=None) -> list[WebElement]:
627632
List[WebElement]
628633
list of `WebElements` matching locator strategy found on the page.
629634
"""
635+
if by == "class name":
636+
if value and any(char.isspace() for char in value.strip()):
637+
raise InvalidSelectorException("Compound class names are not allowed.")
638+
630639
by, value = self._parent.locator_converter.convert(by, value)
640+
631641
return self._execute(Command.FIND_CHILD_ELEMENTS, {"using": by, "value": value})["value"]
632642

633643
def __hash__(self) -> int:

0 commit comments

Comments
 (0)