一、截圖方法
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()