#coding=utf-8
from selenium import webdriver
import time
class SeleniumDriver:
'''
构造方法:
我实例这个对象直接运行,不调用任何方法会运行
'''
def __init__(self,browser):
self.driver = self.open_browser(browser)
'''
二次封装打开浏览器
self:当前实例本身
'''
def open_browser(self,browser):
try:
if browser == 'chrome':
driver = webdriver.Chrome()
elif browser == 'firefox':
driver = webdriver.Firefox()
elif browser == 'ie':
driver = webdriver.Ie()
else:
driver = webdriver.Edge()
time.sleep(1)
self.driver = driver
return driver
except:
print("打开浏览器失败!")
return None
'''
二次封装打开浏览器
'''
def get_url(self,url):
if self.driver != None:
if 'http://' in url:
self.driver.get(url)
else:
print("您的URL有问题!")
else:
print("case失败!")
self.driver.quit()
'''
二次封装浏览器窗口常用操作
args[]:
1.value:
max 最大化
min 最小化
back 后退
forward 前进
refresh 刷新
2.int
'''
def handle_windows(self,*args):
value = len(args)
if value == 1:
if args[0] == 'max':
self.driver.maximize_window()
elif args[0] == 'min':
self.driver.minimize_window()
elif args[0] == 'back':
self.driver.back()
elif args[0] == 'forward':
self.driver.forward()
else:
self.driver.refresh()
elif value == 2:
self.driver.set_window_size(args[0],args[1])
else:
print("您传递的参数有问题!")
time.sleep(5)
self.driver.quit()
selfnium_driver = SeleniumDriver(‘chrome’)
selfnium_driver.handle_windows(‘max’)
使用的是chrome浏览器,运行代码之后可以打开浏览器以及浏览器最大化。
终端显示: