Skip to content

Commit 778d195

Browse files
committed
[py]: Remove warning duplication in webdriver.py and provide stacklevel=
1 parent b4dd762 commit 778d195

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

py/selenium/webdriver/remote/webdriver.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ def get_screenshot_as_file(self, filename) -> bool:
797797
warnings.warn(
798798
"name used for saved screenshot does not match file type. It should end with a `.png` extension",
799799
UserWarning,
800+
stacklevel=2,
800801
)
801802
png = self.get_screenshot_as_png()
802803
try:
@@ -857,8 +858,7 @@ def set_window_size(self, width, height, windowHandle: str = "current") -> None:
857858
858859
driver.set_window_size(800,600)
859860
"""
860-
if windowHandle != "current":
861-
warnings.warn("Only 'current' window is supported for W3C compatible browsers.")
861+
self._check_if_window_handle_is_current(windowHandle)
862862
self.set_window_rect(width=int(width), height=int(height))
863863

864864
def get_window_size(self, windowHandle: str = "current") -> dict:
@@ -870,8 +870,6 @@ def get_window_size(self, windowHandle: str = "current") -> dict:
870870
driver.get_window_size()
871871
"""
872872

873-
if windowHandle != "current":
874-
warnings.warn("Only 'current' window is supported for W3C compatible browsers.")
875873
size = self.get_window_rect()
876874

877875
if size.get("value", None):
@@ -891,8 +889,7 @@ def set_window_position(self, x, y, windowHandle: str = "current") -> dict:
891889
892890
driver.set_window_position(0,0)
893891
"""
894-
if windowHandle != "current":
895-
warnings.warn("Only 'current' window is supported for W3C compatible browsers.")
892+
self._check_if_window_handle_is_current(windowHandle)
896893
return self.set_window_rect(x=int(x), y=int(y))
897894

898895
def get_window_position(self, windowHandle="current") -> dict:
@@ -904,12 +901,16 @@ def get_window_position(self, windowHandle="current") -> dict:
904901
driver.get_window_position()
905902
"""
906903

907-
if windowHandle != "current":
908-
warnings.warn("Only 'current' window is supported for W3C compatible browsers.")
904+
self._check_if_window_handle_is_current(windowHandle)
909905
position = self.get_window_rect()
910906

911907
return {k: position[k] for k in ("x", "y")}
912908

909+
def _check_if_window_handle_is_current(windowHandle: str) -> None:
910+
"""Warns if the window handle is not equal to `current`."""
911+
if windowHandle != "current":
912+
warnings.warn("Only 'current' window is supported for W3C compatible browsers.", stacklevel=2)
913+
913914
def get_window_rect(self) -> dict:
914915
"""Gets the x, y coordinates of the window as well as height and width
915916
of the current window.

py/selenium/webdriver/remote/webelement.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def submit(self):
109109

110110
try:
111111
self._parent.execute_script(script, self)
112-
except JavascriptException:
113-
raise WebDriverException("To submit an element, it must be nested inside a form element")
112+
except JavascriptException as exc:
113+
raise WebDriverException("To submit an element, it must be nested inside a form element") from exc
114114

115115
def clear(self) -> None:
116116
"""Clears the text if it's a text entry element."""

0 commit comments

Comments
 (0)