本章结合前面的装饰器文章,做一次实例操作,头脑风暴,加深对装饰器的理解。
示例1-自动化-打开浏览器操作
# -*- coding: utf-8 -*-
# https://blog.csdn.net/weixin_52040868
# 公众号:测个der
# 微信:qing_an_an
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep
s = Service('F://geckodriver.exe')
driver = webdriver.Firefox(service=s)
driver.get('https://blog.csdn.net/weixin_52040868?spm=1000.2115.3001.5343')
sleep(1)
driver.quit()
装饰器版本
def driver(fun):
def func(args):
driver = webdriver.Firefox(service=args)
return fun(driver)
return func
@driver
def run(driver): # run = driver(run)
driver.get('https://blog.csdn.ne