Skip to content

Commit f7a5f86

Browse files
committed
[py] Move logic to locator converter
1 parent 6699f02 commit f7a5f86

File tree

3 files changed

+5
-20
lines changed

3 files changed

+5
-20
lines changed

py/selenium/webdriver/remote/locator_converter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
18+
from selenium.common.exceptions import InvalidSelectorException
1719
from selenium.webdriver.common.by import By
1820

1921

@@ -23,6 +25,8 @@ def convert(self, by, value):
2325
if by == By.ID:
2426
return By.CSS_SELECTOR, f'[id="{value}"]'
2527
elif by == By.CLASS_NAME:
28+
if value and any(char.isspace() for char in value.strip()):
29+
raise InvalidSelectorException("Compound class names are not allowed.")
2630
return By.CSS_SELECTOR, f".{value}"
2731
elif by == By.NAME:
2832
return By.CSS_SELECTOR, f'[name="{value}"]'

py/selenium/webdriver/remote/webdriver.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
from selenium.common.exceptions import (
3636
InvalidArgumentException,
37-
InvalidSelectorException,
3837
JavascriptException,
3938
NoSuchCookieException,
4039
NoSuchElementException,
@@ -917,10 +916,6 @@ def find_element(self, by=By.ID, value: Optional[str] = None) -> WebElement:
917916
WebElement
918917
The first matching `WebElement` found on the page.
919918
"""
920-
if by == "class name":
921-
if value and any(char.isspace() for char in value.strip()):
922-
raise InvalidSelectorException("Compound class names are not allowed.")
923-
924919
by, value = self.locator_converter.convert(by, value)
925920

926921
if isinstance(by, RelativeBy):
@@ -957,10 +952,6 @@ def find_elements(self, by=By.ID, value: Optional[str] = None) -> list[WebElemen
957952
List[WebElement]
958953
list of `WebElements` matching locator strategy found on the page.
959954
"""
960-
if by == "class name":
961-
if value and any(char.isspace() for char in value.strip()):
962-
raise InvalidSelectorException("Compound class names are not allowed.")
963-
964955
by, value = self.locator_converter.convert(by, value)
965956

966957
if isinstance(by, RelativeBy):

py/selenium/webdriver/remote/webelement.py

Lines changed: 1 addition & 11 deletions
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 InvalidSelectorException, JavascriptException, WebDriverException
28+
from selenium.common.exceptions import JavascriptException, WebDriverException
2929
from selenium.webdriver.common.by import By
3030
from selenium.webdriver.common.utils import keys_to_typing
3131

@@ -598,12 +598,7 @@ 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-
605601
by, value = self._parent.locator_converter.convert(by, value)
606-
607602
return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})["value"]
608603

609604
def find_elements(self, by=By.ID, value=None) -> list[WebElement]:
@@ -632,12 +627,7 @@ def find_elements(self, by=By.ID, value=None) -> list[WebElement]:
632627
List[WebElement]
633628
list of `WebElements` matching locator strategy found on the page.
634629
"""
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-
639630
by, value = self._parent.locator_converter.convert(by, value)
640-
641631
return self._execute(Command.FIND_CHILD_ELEMENTS, {"using": by, "value": value})["value"]
642632

643633
def __hash__(self) -> int:

0 commit comments

Comments
 (0)