一、知識點:
maximize_window()
fullscreen_window()
minimize_window()
set_window_size()
get_window_size()
set_window_position()
get_window_position()
set_window_rect()
get_window_rect()
## 二、示例:瀏覽器大小和位置.py ``` # encoding:utf-8 from selenium import webdriver from time import sleep
1、打開 谷歌瀏覽器
driver = webdriver.Chrome()
2、最大化瀏覽器
driver.maximize_window()
sleep(2)
3、瀏覽器全屏化
driver.fullscreen_window()
sleep(2)
4、最小化瀏覽器
driver.minimize_window()
sleep(2)
5、設置瀏覽器大小為800*600
driver.set_window_size(800, 600)
sleep(2)
6、獲取瀏覽器大小
window_size = driver.get_window_size()
print('瀏覽器大小:', window_size)
sleep(2)
7、設置瀏覽器位置
driver.set_window_position(720, 400)
sleep(2)
8、獲取瀏覽器位置
window_position = driver.get_window_position()
print('瀏覽器位置:', window_position)
sleep(2)
9、設置瀏覽器的大小 600*400 和位置 600,500
driver.set_window_rect(x=600, y=500, width=600, height=400)
sleep(2)
10、獲取瀏覽器的大小和位置
size_and_position = driver.get_window_rect()
print('瀏覽器的大小和位置:', size_and_position)
<br>
## 三、相關源碼:
### \site-packages\selenium\webdriver\remote\webdriver.py
def maximize_window(self):
"""
Maximizes the current window that webdriver is using
最大化webdriver正在使用的當前窗口
"""
params = None
command = Command.W3C_MAXIMIZE_WINDOW
if not self.w3c:
command = Command.MAXIMIZE_WINDOW
params = {'windowHandle': 'current'}
self.execute(command, params)
def fullscreen_window(self):
"""
Invokes the window manager-specific 'full screen' operation
調用特定於窗口管理器的“全屏”操作
"""
self.execute(Command.FULLSCREEN_WINDOW)
def minimize_window(self):
"""
Invokes the window manager-specific 'minimize' operation
調用特定於窗口管理器的“最小化”操作
"""
self.execute(Command.MINIMIZE_WINDOW)
def set_window_size(self, width, height, windowHandle='current'):
"""
Sets the width and height of the current window. (window.resizeTo)
設置當前窗口的寬度和高度
:Args:
- width: the width in pixels to set the window to
設置窗口的寬度(以像素為單位)
- height: the height in pixels to set the window to
設置窗口的高度(以像素為單位)
:Usage:
driver.set_window_size(800,600)
"""
if self.w3c:
if windowHandle != 'current':
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
self.set_window_rect(width=int(width), height=int(height))
else:
self.execute(Command.SET_WINDOW_SIZE, {
'width': int(width),
'height': int(height),
'windowHandle': windowHandle})
def get_window_size(self, windowHandle='current'):
"""
Gets the width and height of the current window.
獲取當前窗口的寬度和高度
:Usage:
driver.get_window_size()
"""
command = Command.GET_WINDOW_SIZE
if self.w3c:
if windowHandle != 'current':
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
size = self.get_window_rect()
else:
size = self.execute(command, {'windowHandle': windowHandle})
if size.get('value', None) is not None:
size = size['value']
return {k: size[k] for k in ('width', 'height')}
def set_window_position(self, x, y, windowHandle='current'):
"""
Sets the x,y position of the current window. (window.moveTo)
設置當前窗口的x,y位置
:Args:
- x: the x-coordinate in pixels to set the window position
用於設置窗口位置的x坐標(以像素為單位)
- y: the y-coordinate in pixels to set the window position
用於設置窗口位置的y坐標(以像素為單位)
:Usage:
driver.set_window_position(0,0)
"""
if self.w3c:
if windowHandle != 'current':
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
return self.set_window_rect(x=int(x), y=int(y))
else:
self.execute(Command.SET_WINDOW_POSITION,
{
'x': int(x),
'y': int(y),
'windowHandle': windowHandle
})
def get_window_position(self, windowHandle='current'):
"""
Gets the x,y position of the current window.
獲取當前窗口的x,y位置
:Usage:
driver.get_window_position()
"""
if self.w3c:
if windowHandle != 'current':
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
position = self.get_window_rect()
else:
position = self.execute(Command.GET_WINDOW_POSITION,
{'windowHandle': windowHandle})['value']
return {k: position[k] for k in ('x', 'y')}
def get_window_rect(self):
"""
Gets the x, y coordinates of the window as well as height and width of
the current window.
獲取窗口的x,y坐標以及當前窗口的高度和寬度
:Usage:
driver.get_window_rect()
"""
return self.execute(Command.GET_WINDOW_RECT)['value']
def set_window_rect(self, x=None, y=None, width=None, height=None):
"""
Sets the x, y coordinates of the window as well as height and width of
the current window.
設置窗口的x,y坐標以及當前窗口的高度和寬度
:Usage:
driver.set_window_rect(x=10, y=10)
driver.set_window_rect(width=100, height=200)
driver.set_window_rect(x=10, y=10, width=100, height=200)
"""
if (x is None and y is None) and (height is None and width is None):
raise InvalidArgumentException("x and y or height and width need values")
return self.execute(Command.SET_WINDOW_RECT, {"x": x, "y": y,
"width": width,
"height": height})['value']
<br>
<br>
<br>
<br>
<br>
<br>