Skip to content

Commit de1275d

Browse files
[py] Add get/set window rect commands
1 parent 6014af6 commit de1275d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

py/selenium/webdriver/remote/command.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class Command(object):
6363
GET_WINDOW_POSITION = "getWindowPosition"
6464
SET_WINDOW_SIZE = "setWindowSize"
6565
W3C_SET_WINDOW_SIZE = "w3cSetWindowSize"
66+
SET_WINDOW_RECT = "setWindowRect"
67+
GET_WINDOW_RECT = "getWindowRect"
6668
SET_WINDOW_POSITION = "setWindowPosition"
6769
W3C_SET_WINDOW_POSITION = "w3cSetWindowPosition"
6870
SWITCH_TO_WINDOW = "switchToWindow"

py/selenium/webdriver/remote/remote_connection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=True):
348348
('GET', '/session/$sessionId/window/position'),
349349
Command.W3C_SET_WINDOW_POSITION:
350350
('POST', '/session/$sessionId/window/position'),
351+
Command.SET_WINDOW_RECT:
352+
('POST', '/session/$sessionId/window/rect'),
353+
Command.GET_WINDOW_RECT:
354+
('GET', '/session/$sessionId/window/rect'),
351355
Command.MAXIMIZE_WINDOW:
352356
('POST', '/session/$sessionId/window/$windowHandle/maximize'),
353357
Command.W3C_MAXIMIZE_WINDOW:

py/selenium/webdriver/remote/webdriver.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
from .switch_to import SwitchTo
2929
from .mobile import Mobile
3030
from .file_detector import FileDetector, LocalFileDetector
31-
from selenium.common.exceptions import WebDriverException
31+
from selenium.common.exceptions import (InvalidArgumentException,
32+
WebDriverException)
3233
from selenium.webdriver.common.by import By
3334
from selenium.webdriver.common.html5.application_cache import ApplicationCache
3435

@@ -903,6 +904,32 @@ def get_window_position(self, windowHandle='current'):
903904
return self.execute(Command.GET_WINDOW_POSITION, {
904905
'windowHandle': windowHandle})['value']
905906

907+
def get_window_rect(self):
908+
"""
909+
Gets the x, y coordinates of the window as well as height and width of
910+
the current window.
911+
912+
:Usage:
913+
driver.get_window_rect()
914+
"""
915+
return self.execute(Command.GET_WINDOW_RECT)['value']
916+
917+
def set_window_rect(self, x=None, y=None, width=None, height=None):
918+
"""
919+
Sets the x, y coordinates of the window as well as height and width of
920+
the current window.
921+
922+
:Usage:
923+
driver.set_window_rect(x=10, y=10)
924+
driver.set_window_rect(width=100, height=200)
925+
driver.set_window_rect(x=10, y=10, width=100, height=200)
926+
"""
927+
if (x is None and y is None) and (height is None and width is None):
928+
raise InvalidArgumentException("x and y or height and width need values")
929+
930+
return self.execute(Command.SET_WINDOW_RECT, {"x": x, "y": y,
931+
"width": width,
932+
"height": height})['value']
906933
@property
907934
def file_detector(self):
908935
return self._file_detector

0 commit comments

Comments
 (0)