一、知识点:
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>