一、截图方法
1.get_screenshot_as_file(self, filename)
--这个方法是获取当前window的截图,出现IOError时候返回False,截图成功返回True。
filename参数是保存文件的路径。
driver.get_screenshot_as_file('/Screenshots/foo.png')
2.get_screenshot_as_base64(self)
--这个方法也是获取屏幕截图,保存的是base64的编码格式,在HTML界面输出截图的时候,会用到。
比如,想把截图放到html测试报告里。
driver.get_screenshot_as_base64()
3.get_screenshot_as_png(self)
--这个是获取屏幕截图,保存的是二进制数据,很少用到.
driver.get_screenshot_as_png()
#!/usr/bin/env python
# coding: utf-8
import os, time
from loginfo.log_print import get_log
from selenium import webdriver
def screenshot(driver):
# 设置截图文件保存的路径
path = 'F:\\Test\\loginfo'
path = os.path.join(path, 'ScreenShot')
if not os.path.exists(path):
os.mkdir(path)
# 设置要截图的文件名
file_name = time.strftime('%Y_%m_%d_%H_%M_%S', time.localtime()) + '.png'
path = os.path.join(path, file_name)
log = get_log('screenshot')
try:
driver.get_screenshot_as_file(path)
log.info('截图成功')
except OSError:
log.info('截图失败')
if __name__ == '__main__':
driver0 = webdriver.Chrome()
screenshot(driver0)
driver0.quit()