Webdriver自带截图功能有两种,get_screenshot_as_file(),save_screenshot(),一般我习惯使用前者。使用这两种方式截图时传入文件存放路径名称即可截图成功。但是每一次截图时需要传入一个文件名,并且多次命名可能存在重复或者毫无章法。因此可以采用自动生成日期、时间来命名文件夹和文件名称可方便管理截图,并且调用截图函数时,不需要一次次的传参。
语法:os.path.dirname(path)
功能:去掉文件名,返回目录
语法:time.strftime(format[,t])
功能:返回可读字符串表示的当地时间
获取当前目录:
1、os.getcwd()
2、os.path.abspath(os.path.dirname(__file__))
获取上级目录:
1、os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
2、os.path.abspath(os.path.dirname(os.getcwd()))
3、os.path.abspath(os.path.join(os.getcwd(), ".."))
获取上上级目录
1、print os.path.abspath(os.path.join(os.getcwd(), "../.."))
我这里生成在上级目录,以下为代码示例:
def screenshot(self):
# 生成年月份时分秒时间
picture_time = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))
print(picture_time)
directory_time = time.strftime('%Y-%m-%d', time.
web自动化测试——webdriver截图优化:如何生成日期目录,并将截图以时间命名保存于日期文件夹中
最新推荐文章于 2023-03-27 11:00:02 发布