From bda34a7faaaa8a42afb0346e24ef00c10537f6fd Mon Sep 17 00:00:00 2001 From: pallavigotwork Date: Mon, 8 Sep 2025 14:18:39 +0530 Subject: [PATCH 1/5] fixed mypy errors in switch_to file and relative locator file --- py/selenium/webdriver/remote/switch_to.py | 2 +- py/selenium/webdriver/support/relative_locator.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/py/selenium/webdriver/remote/switch_to.py b/py/selenium/webdriver/remote/switch_to.py index 1fad0f74a0d68..2a758ab58da33 100644 --- a/py/selenium/webdriver/remote/switch_to.py +++ b/py/selenium/webdriver/remote/switch_to.py @@ -87,7 +87,7 @@ def frame(self, frame_reference: Union[str, int, WebElement]) -> None: try: frame_reference = self._driver.find_element(By.NAME, frame_reference) except NoSuchElementException as exc: - raise NoSuchFrameException(frame_reference) from exc + raise NoSuchFrameException(str(frame_reference)) from exc self._driver.execute(Command.SWITCH_TO_FRAME, {"id": frame_reference}) diff --git a/py/selenium/webdriver/support/relative_locator.py b/py/selenium/webdriver/support/relative_locator.py index 5962ae128c874..9022331838839 100644 --- a/py/selenium/webdriver/support/relative_locator.py +++ b/py/selenium/webdriver/support/relative_locator.py @@ -18,7 +18,7 @@ from typing import NoReturn, Optional, Union, overload from selenium.common.exceptions import WebDriverException -from selenium.webdriver.common.by import By, ByType +from selenium.webdriver.common.by import ByType from selenium.webdriver.remote.webelement import WebElement @@ -48,7 +48,7 @@ def with_tag_name(tag_name: str) -> "RelativeBy": warnings.warn("This method is deprecated and may be removed in future versions. Please use `locate_with` instead.") if not tag_name: raise WebDriverException("tag_name can not be null") - return RelativeBy({By.CSS_SELECTOR: tag_name}) + return RelativeBy({"css selector": tag_name}) def locate_with(by: ByType, using: str) -> "RelativeBy": From c911b181287bce8166c1a4c20c5e758933874518 Mon Sep 17 00:00:00 2001 From: pallavigotwork Date: Tue, 9 Sep 2025 21:13:15 +0530 Subject: [PATCH 2/5] stored frame into another var and passed that in exception --- py/selenium/webdriver/remote/switch_to.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/py/selenium/webdriver/remote/switch_to.py b/py/selenium/webdriver/remote/switch_to.py index 2a758ab58da33..d07087d369888 100644 --- a/py/selenium/webdriver/remote/switch_to.py +++ b/py/selenium/webdriver/remote/switch_to.py @@ -81,13 +81,14 @@ def frame(self, frame_reference: Union[str, int, WebElement]) -> None: driver.switch_to.frame(driver.find_elements(By.TAG_NAME, "iframe")[0]) """ if isinstance(frame_reference, str): + frame_name = frame_reference try: frame_reference = self._driver.find_element(By.ID, frame_reference) except NoSuchElementException: try: frame_reference = self._driver.find_element(By.NAME, frame_reference) except NoSuchElementException as exc: - raise NoSuchFrameException(str(frame_reference)) from exc + raise NoSuchFrameException(frame_name) from exc self._driver.execute(Command.SWITCH_TO_FRAME, {"id": frame_reference}) From 06d3045509daaf04b7121809766405999a47e50f Mon Sep 17 00:00:00 2001 From: pallavigotwork Date: Wed, 17 Sep 2025 18:07:19 +0530 Subject: [PATCH 3/5] moved the bytype at module level, fixed the imports, reverted the return type --- py/selenium/webdriver/common/by.py | 20 ++++++++++--------- .../webdriver/support/relative_locator.py | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/py/selenium/webdriver/common/by.py b/py/selenium/webdriver/common/by.py index c26b6ea62db05..65431949482ed 100644 --- a/py/selenium/webdriver/common/by.py +++ b/py/selenium/webdriver/common/by.py @@ -18,6 +18,7 @@ from typing import Literal, Optional +ByType = Literal["id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector"] class By: """Set of supported locator strategies. @@ -73,14 +74,15 @@ class By: >>> element = driver.find_element(By.CSS_SELECTOR, "div.myElement") """ - ID = "id" - XPATH = "xpath" - LINK_TEXT = "link text" - PARTIAL_LINK_TEXT = "partial link text" - NAME = "name" - TAG_NAME = "tag name" - CLASS_NAME = "class name" - CSS_SELECTOR = "css selector" + + ID: ByType = "id" + XPATH: ByType = "xpath" + LINK_TEXT: ByType = "link text" + PARTIAL_LINK_TEXT: ByType = "partial link text" + NAME: ByType = "name" + TAG_NAME: ByType = "tag name" + CLASS_NAME: ByType = "class name" + CSS_SELECTOR: ByType = "css selector" _custom_finders: dict[str, str] = {} @@ -97,4 +99,4 @@ def clear_custom_finders(cls) -> None: cls._custom_finders.clear() -ByType = Literal["id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector"] + diff --git a/py/selenium/webdriver/support/relative_locator.py b/py/selenium/webdriver/support/relative_locator.py index 9022331838839..2d429ac15ded2 100644 --- a/py/selenium/webdriver/support/relative_locator.py +++ b/py/selenium/webdriver/support/relative_locator.py @@ -19,6 +19,7 @@ from selenium.common.exceptions import WebDriverException from selenium.webdriver.common.by import ByType +from selenium.webdriver.common.by import By from selenium.webdriver.remote.webelement import WebElement @@ -48,8 +49,7 @@ def with_tag_name(tag_name: str) -> "RelativeBy": warnings.warn("This method is deprecated and may be removed in future versions. Please use `locate_with` instead.") if not tag_name: raise WebDriverException("tag_name can not be null") - return RelativeBy({"css selector": tag_name}) - + return RelativeBy({By.CSS_SELECTOR: tag_name}) def locate_with(by: ByType, using: str) -> "RelativeBy": """Start searching for relative objects your search criteria with By. From d2f22ce4d69f1684647af5ebad60ea05ec2d6493 Mon Sep 17 00:00:00 2001 From: pallavigotwork Date: Wed, 17 Sep 2025 18:08:55 +0530 Subject: [PATCH 4/5] reverted return type, moved bytype to module level, added by import --- py/selenium/webdriver/common/by.py | 5 +---- py/selenium/webdriver/support/relative_locator.py | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/py/selenium/webdriver/common/by.py b/py/selenium/webdriver/common/by.py index 65431949482ed..986099c7bb414 100644 --- a/py/selenium/webdriver/common/by.py +++ b/py/selenium/webdriver/common/by.py @@ -20,6 +20,7 @@ ByType = Literal["id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector"] + class By: """Set of supported locator strategies. @@ -74,7 +75,6 @@ class By: >>> element = driver.find_element(By.CSS_SELECTOR, "div.myElement") """ - ID: ByType = "id" XPATH: ByType = "xpath" LINK_TEXT: ByType = "link text" @@ -97,6 +97,3 @@ def get_finder(cls, name: str) -> Optional[str]: @classmethod def clear_custom_finders(cls) -> None: cls._custom_finders.clear() - - - diff --git a/py/selenium/webdriver/support/relative_locator.py b/py/selenium/webdriver/support/relative_locator.py index 2d429ac15ded2..5962ae128c874 100644 --- a/py/selenium/webdriver/support/relative_locator.py +++ b/py/selenium/webdriver/support/relative_locator.py @@ -18,8 +18,7 @@ from typing import NoReturn, Optional, Union, overload from selenium.common.exceptions import WebDriverException -from selenium.webdriver.common.by import ByType -from selenium.webdriver.common.by import By +from selenium.webdriver.common.by import By, ByType from selenium.webdriver.remote.webelement import WebElement @@ -51,6 +50,7 @@ def with_tag_name(tag_name: str) -> "RelativeBy": raise WebDriverException("tag_name can not be null") return RelativeBy({By.CSS_SELECTOR: tag_name}) + def locate_with(by: ByType, using: str) -> "RelativeBy": """Start searching for relative objects your search criteria with By. From 185ad8932a4f771efff21c6ee93c12b8703b0b68 Mon Sep 17 00:00:00 2001 From: pallavigotwork Date: Wed, 24 Sep 2025 14:05:47 +0530 Subject: [PATCH 5/5] reverted frame exception obj, changed type from str to Any in the common exception file --- py/selenium/common/exceptions.py | 24 +++++++++++------------ py/selenium/webdriver/remote/switch_to.py | 3 +-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/py/selenium/common/exceptions.py b/py/selenium/common/exceptions.py index 80c37ef4680e5..7e9b028a822fc 100644 --- a/py/selenium/common/exceptions.py +++ b/py/selenium/common/exceptions.py @@ -17,7 +17,7 @@ """Exceptions that may happen in all the webdriver code.""" from collections.abc import Sequence -from typing import Optional +from typing import Any, Optional SUPPORT_MSG = "For documentation on this error, please visit:" ERROR_URL = "https://www.selenium.dev/documentation/webdriver/troubleshooting/errors" @@ -27,7 +27,7 @@ class WebDriverException(Exception): """Base webdriver exception.""" def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: super().__init__() self.msg = msg @@ -73,7 +73,7 @@ class NoSuchElementException(WebDriverException): """ def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#nosuchelementexception" @@ -112,7 +112,7 @@ class StaleElementReferenceException(WebDriverException): """ def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#staleelementreferenceexception" @@ -137,7 +137,7 @@ class UnexpectedAlertPresentException(WebDriverException): def __init__( self, - msg: Optional[str] = None, + msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None, alert_text: Optional[str] = None, @@ -166,7 +166,7 @@ class ElementNotVisibleException(InvalidElementStateException): """ def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementnotvisibleexception" @@ -178,7 +178,7 @@ class ElementNotInteractableException(InvalidElementStateException): element will hit another element due to paint order.""" def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementnotinteractableexception" @@ -225,7 +225,7 @@ class InvalidSelectorException(WebDriverException): """ def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#invalidselectorexception" @@ -267,7 +267,7 @@ class ElementClickInterceptedException(WebDriverException): clicked.""" def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementclickinterceptedexception" @@ -288,7 +288,7 @@ class InvalidSessionIdException(WebDriverException): meaning the session either does not exist or that it's not active.""" def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#invalidsessionidexception" @@ -299,7 +299,7 @@ class SessionNotCreatedException(WebDriverException): """A new session could not be created.""" def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#sessionnotcreatedexception" @@ -315,7 +315,7 @@ class NoSuchDriverException(WebDriverException): """Raised when driver is not specified and cannot be located.""" def __init__( - self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None + self, msg: Optional[Any] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None ) -> None: with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}/driver_location" diff --git a/py/selenium/webdriver/remote/switch_to.py b/py/selenium/webdriver/remote/switch_to.py index 26772f2c48263..30a26ed760f39 100644 --- a/py/selenium/webdriver/remote/switch_to.py +++ b/py/selenium/webdriver/remote/switch_to.py @@ -80,14 +80,13 @@ def frame(self, frame_reference: Union[str, int, WebElement]) -> None: driver.switch_to.frame(driver.find_elements(By.TAG_NAME, "iframe")[0]) """ if isinstance(frame_reference, str): - frame_name = frame_reference try: frame_reference = self._driver.find_element(By.ID, frame_reference) except NoSuchElementException: try: frame_reference = self._driver.find_element(By.NAME, frame_reference) except NoSuchElementException as exc: - raise NoSuchFrameException(frame_name) from exc + raise NoSuchFrameException(frame_reference) from exc self._driver.execute(Command.SWITCH_TO_FRAME, {"id": frame_reference})