根據Python Selenium文檔,driver.save_screenshot
如果遇到IOError
,則返回False
。如果保存失敗,我如何知道錯誤是什么?
Example code:
if driver.save_screenshot(screenshot_path):
print(f"Screenshot saved at {screenshot_path}")
else:
# Let the devops engineer know why the screenshot couldn't be saved
print("Screenshot save failed")
此方法不讓您知道在保存文件時代碼遇到了什么樣的問題。
您可以實現自己的。使用
get_screenshot_as_png()
或get_screenshot_as_base64()
方法,使用標準機制向文件寫入字節,并捕獲可能的異常。