Python+Selenium--异常截图

前言

     Webdriver 提供错误截图函数get_screenshot_as_file(),可以帮助我们跟踪bug,在脚本无法继续执行时候, get_screenshot_as_file()函数将截取当前页面的截图保存到指定的位置,这是一个非常棒的功能,下面实例展示get_screenshot_as_file()函数的使用。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 异常截图.py
@time: 2018-09-28 15:34
@desc:
'''
from selenium import webdriver
 
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
#捕捉百度输入框异常
try:
    driver.find_element_by_id("kwsss").send_keys("uniquefu")
    driver.find_element_by_id("su").click()
except Exception as e:
    print(e)
    driver.get_screenshot_as_file("f:/error_png.png")
 
driver.quit()

  打开f:/ 我们将看到生成的error_png.png 文件.

原文地址:https://www.cnblogs.com/chenlimei/p/12781011.html