Python+Selenium自动化测试框架--控制浏览器(窗口大小、后退、前进)

一、控制浏览器的窗口:

# coding=utf-8
from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("http:www.baidu.com")

try:
    print("测试全屏模式")
    driver.maximize_window()
    time.sleep(3)
    print(driver.get_window_size())
    print("测试最小化")
    driver.minimize_window()
    time.sleep(3)
    print(driver.get_window_size())
    print("测试设置浏览器宽1024、高768显示")
    driver.set_window_size(1024, 768)
    time.sleep(3)
    print(driver.get_window_size())
except Exception as e:
    print("控制浏览器失败", format(e))

driver.quit()

通过上述实例得知:

全屏模式:maximize_window() 、最小化模式:minimize_window() ,都不需要传参

窗口大小控制:set_window_size() ,参数数字为像素点

获取窗口像素:get_window_size()

二、浏览器的前进、后退

# coding=utf-8
from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(5)

url1 = "http://www.baidu.com"
url2 = "http://news.baidu.com/"
driver.get(url1)
print(f"现在访问的是:{url1}")
driver.get(url2)
print("现在访问的是:%s" % url2)
driver.back()
print(f"执行后退操作,URL:{url1}")
driver.forward()
print(f"执行前进操作,URL:{url2}")

driver.quit()

通过上述实例得知:

前进:forward()   后退:back()

相关推荐