from selenium import webdriver
import time
import requests
# 类使用
class SeleniumDriver:
# 弄一个构造方法:(实例Selenium时就会运行这个构造方法)弄成全局,将driver
def __init__(self, browser):
self.driver = self.open_browser(browser)
s = requests.session()
s.keep_alive = False
# 打开浏览器
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)
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()
# 操作浏览器
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] == 'go':
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()
selenium_driver = SeleniumDriver('chrome')
selenium_driver.get_url("http://www.baidu.com")
print("ssss")
selenium_driver.handle_windows('go')