Selenium3 Python WebDriver API源码探析(5):浏览器导航、页面基本信息(URL、标题、HTML)、窗口大小位置管理

Selenium对于浏览器的操控主要通过selenium\webdriver\remote\webdriver.pyWebDriver类的相关方法和特性(property)实现。

首先,创建WebDriver实例

from selenium import webdriver

driver = webdriver.Firefox()

浏览器导航

打开页面

作用:根据url打开页面
签名:driver.get("http://www.baidu.com")

get方法源码:

def get(self, url):
    self.execute(Command.GET, {
    
    'url': url})

后退

作用:相当于点击浏览器后退按钮。
签名:driver.back()

back方法源码:

def forward(self):
    self.execute(Command.GO_FORWARD)

前进

作用:相当于点击浏览器前进按钮。
签名:driver.forward()

forward方法源码:

def forward(self):
    self.execute(Command.GO_FORWARD)

刷新

相当于点击浏览器刷新按钮。
driver.refresh()

refresh方法源码:

def refresh(self):
    self.execute(Command.REFRESH)

页面基本信息

获取当前页面URL

作用:获取当前页面URL
签名:driver.current_url

current_url特性源码:

@property
def current_url(self):
    return self.execute(Command.GET_CURRENT_URL)['value']

获取当前页面标题

作用:获取当前页面标题
签名:driver.title

title特性源码:

@property
def title(self):
    resp = self.execute(Command.GET_TITLE)
    return resp['value'] if resp['value'] is not None else ""

获取当前页面HTML

作用:获取当前页面HTML
签名:driver.page_source

page_source特性源码:

def page_source(self):
    return self.execute(Command.GET_PAGE_SOURCE)['value']

窗口大小、位置管理

获取窗口大小

作用:获取浏览器窗口的大小(以像素为单位)。
签名: driver.get_window_size()
返回值:字典,例如:{'width': 1296, 'height': 821}
get_window_size方法源码:

def get_window_size(self, windowHandle='current'):
    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')}

设置窗口大小

作用:设置浏览器窗口的大小,单位为像素。
签名: driver.set_window_size(width, height)
返回值:None
set_window_size方法源码:

def set_window_size(self, width, height, windowHandle='current'):
    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})

获取窗口位置

作用:获取浏览器窗口左上角的坐标,单位为像素。
签名: driver.get_window_position()
返回值:字典,例如{'x': 4, 'y': 4}
get_window_position方法源码:

def get_window_position(self, windowHandle='current'):
    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')}

设置窗口位置

作用:将窗口移动到设定的位置,单位为像素。
签名: driver.set_window_position(x, y)
返回值:字典,例如:{'x': 1, 'y': 1, 'width': 1296, 'height': 821}
set_window_position方法源码:

def set_window_position(self, x, y, windowHandle='current'):
    """
    Sets the x,y position of the current window. (window.moveTo)

    :Args:
        - x: the x-coordinate in pixels to set the window position
        - y: the y-coordinate in pixels to set the window position

    :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
                        })

最大化窗口

作用:最大化窗口。
签名: driver.maximize_window()
返回值:None
maximize_window方法源码:

def maximize_window(self):
    """
    Maximizes the current window that webdriver is using
    """
    params = None
    command = Command.W3C_MAXIMIZE_WINDOW
    if not self.w3c:
        command = Command.MAXIMIZE_WINDOW
        params = {
    
    'windowHandle': 'current'}
    self.execute(command, params)

最小化窗口

作用:最小化窗口。
签名: driver.minimize_window()
返回值:None
minimize_window方法源码:

def minimize_window(self):
    """
    Invokes the window manager-specific 'minimize' operation
    """
    self.execute(Command.MINIMIZE_WINDOW)

全屏窗口

作用:全屏窗口,类似于按下F11键。
签名: driver.fullscreen_window()
返回值:None
fullscreen_window方法源码:

def fullscreen_window(self):
    """
    Invokes the window manager-specific 'full screen' operation
    """
    self.execute(Command.FULLSCREEN_WINDOW)

猜你喜欢

转载自blog.csdn.net/mighty13/article/details/114673369