日志模块可以通过封装一个类,也可以通过配置文件取管理
新建1个log.ini文件
[loggers] keys=root [handlers] keys=fileHandler,streamHandler [formatters] keys=simpleFormatter [logger_root] level=INFO handlers=fileHandler,streamHandler [handler_fileHandler] class=FileHandler level=INFO formatter=simpleFormatter args=('mylog.log','a','utf-8') [handler_streamHandler] class=StreamHandler level=INFO formatter=simpleFormatter [formatter_simpleFormatter] format=%(asctime)s %(filename)s %(levelname)s %(funcName)s %(message)s
在程序中进行调用
''' 使用配置文件,进行设置日志 ''' import logging.config import time # 拿到配置文件 logging.config.fileConfig('../Data/log.ini') # 拿到日志器 log =logging.getLogger() class BasePage: # 想要把操作记录在日志文件中。调用日志中的级别 def __init__(self, driver): log.info('初始化driver{}'.format(driver)) self.driver = driver def open(self, url): log.info('正在访问网址{}'.format(url)) self.driver.get(url) def locator(self, name, value): return self.driver.find_element(name, value) def on_input(self, name, value, txt): try: log.info('正在定位{}元素,元素值为{},输入的内容为{}'.format(name, value, txt)) self.locator(name, value).send_keys(txt) except Exception as e: log.error('输入内容失败%s' % e) def on_click(self, name, value): try: log.info('正在定位{}元素,元素值为{},进行点击'.format(name, value)) self.locator(name, value).click() except Exception as e: log.error('点击按钮失败%s' % e) def wait(self, t): log.info('正在等待') time.sleep(t) def close(self): log.info('关闭浏览器') self.driver.quit()
进行使用:
# from Study.Practices.practice5 import BasePage from Study.Practices.BaseLog import BasePage from selenium import webdriver driver=BasePage(webdriver.Chrome()) driver.open('http://www.baidu.com') driver.on_input('id','kw','秋水') driver.on_click('id','su') driver.wait(3) driver.on_click('xpath','//*[@id="1"]/h3/a/em') driver.wait(3) driver.close()
控制台产生日志
C:\Users\ceshi001\PycharmProjects\pythonstudy\venv\Scripts\python.exe C:/Users/ceshi001/PycharmProjects/pythonstudy/Study/Practices/practice2.py 2021-04-28 15:39:02,159 BaseLog.py INFO __init__ 初始化driver<selenium.webdriver.chrome.webdriver.WebDriver (session="520f01d9b7f6aca1b0d1d4f8ce54c39f")> 2021-04-28 15:39:02,159 BaseLog.py INFO open 正在访问网址http://www.baidu.com 2021-04-28 15:39:02,810 BaseLog.py INFO on_input 正在定位id元素,元素值为kw,输入的内容为秋水 2021-04-28 15:39:02,909 BaseLog.py INFO on_click 正在定位id元素,元素值为su,进行点击 2021-04-28 15:39:03,011 BaseLog.py INFO wait 正在等待 2021-04-28 15:39:06,012 BaseLog.py INFO on_click 正在定位xpath元素,元素值为//*[@id="1"]/h3/a/em,进行点击 2021-04-28 15:39:06,078 BaseLog.py INFO wait 正在等待 2021-04-28 15:39:09,080 BaseLog.py INFO close 关闭浏览器 Process finished with exit code 0
文件中产生日志:
2021-04-28 15:39:02,159 BaseLog.py INFO __init__ 初始化driver<selenium.webdriver.chrome.webdriver.WebDriver (session="520f01d9b7f6aca1b0d1d4f8ce54c39f")> 2021-04-28 15:39:02,159 BaseLog.py INFO open 正在访问网址http://www.baidu.com 2021-04-28 15:39:02,810 BaseLog.py INFO on_input 正在定位id元素,元素值为kw,输入的内容为秋水 2021-04-28 15:39:02,909 BaseLog.py INFO on_click 正在定位id元素,元素值为su,进行点击 2021-04-28 15:39:03,011 BaseLog.py INFO wait 正在等待 2021-04-28 15:39:06,012 BaseLog.py INFO on_click 正在定位xpath元素,元素值为//*[@id="1"]/h3/a/em,进行点击 2021-04-28 15:39:06,078 BaseLog.py INFO wait 正在等待 2021-04-28 15:39:09,080 BaseLog.py INFO close 关闭浏览器