selenium支持对当前页面保存截图,使用方法:
driver.get_screenshot_as_file(file_path)
代码举例:
......
def get_screenshot(driver, path=''):'''保存页面截图'''n = 0if path:passelse:path = "D:\\test_data\\screenshot\\error_png.png"'''检查文件是否已存在'''while (os.path.exists(path)):# print("文件已存在:" + path)n += 1path = "D:\\test_data\\screenshot\\error_png" + str(n) + ".png"driver.get_screenshot_as_file(path) # 保存截图driver.implicitly_wait(2)print("已保存截图,路径为:" + path)
......