zoukankan      html  css  js  c++  java
  • selenium 截图加上时间戳

    思路:

    1  新建screenshot文件夹,不存在则创建该目录

    2  在screenshot文件夹下新建当日日期文件夹,比如20190110;不存在则创建该目录

    3  截图保存到当日文件夹,且截图文件名加上时间戳。 避免用例执行多次,截图被覆盖。

     

    import os
    from datetime import datetime
    
    
    def getscreenshot(driver, filename="页面截图"): 
        """带有时间戳的截图"""
        screenshot_dir = './screenshot'  # 当前目录下的screenshot文件夹;或设置其他目录
        if not os.path.exists(screenshot_dir):  # 不存在则创建该目录
            os.mkdir(screenshot_dir)
    
        nowdate = datetime.now().strftime('%Y%m%d')  # 当日日期
        screenshot_today_dir = os.path.join(screenshot_dir, nowdate)  # 当前日期文件夹
        if not os.path.exists(screenshot_today_dir):
            os.mkdir(screenshot_today_dir)  # 不存在则创建
    
        nowtime = datetime.now().strftime('%H%M%S%f')  # 时间戳
        filename = nowtime + filename + ".png"  # 拼接文件名 时间戳+文件名+.png
        filepath = os.path.join(screenshot_today_dir, filename)
    driver.get_screenshot_as_file(filepath)
    # 截图,文件名=filename+时间戳

    调用:

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    getscreenshot(driver)
    # getscreenshot(driver,'走势图异常')

    运行后:

    the end!

  • 相关阅读:
    Web Service简介
    初识web service
    HTTP 状态代码的完整列表
    【转】JBPM4 RepositoryService
    【转】SD和SDHC和SDXC卡的区别
    【转】PP常用TCODE
    【转】SAP会计科目表
    【转】JBPM4中 state 和 task 的不同
    【转】学习一下车险
    【转】JBPM4 TaskService
  • 原文地址:https://www.cnblogs.com/dinghanhua/p/10252297.html
Copyright © 2011-2022 走看看