[Python] 怎么把HTML的报告转换为图片,利用无头浏览器
How to convert HTML Report to picture format in Email? So that we can see the automation report also at home or on mobile phone anywhere.
We tried to use phantomJs to get the full-page screenshot of HTML, it doesn't work well on some computers, then we found that the newest Chrome doesn't support it anymore, and Chrome has use its Headless mode to replace phantomJs.
Version 1 : phantomJs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # -*- coding: utf- 8 -*- import time import os from selenium import webdriver jenkinsJobName=os.getenv( "JOB_NAME" ) url= "http://10.2.3.3/testAgent/AutoAnaylsisReport.html" print url save_fn= "buildNumResult.PNG" driver = webdriver.PhantomJS() driver.maximize_window() driver. get (url) # Load page time.sleep( 30 ) driver.save_screenshot(save_fn) driver.close() time.sleep( 5 ) os.system( "taskkill /F /IM phantomjs.exe" ) |
Version 2: Chrome Headless
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # -*- coding: utf- 8 -*- import time import os from selenium import webdriver from selenium.webdriver.common.keys import Keys url= "http://10.2.4.1/testAgent/BillingAnaylisisReport.html" print url save_fn= "buildNumResult.PNG" option = webdriver.ChromeOptions() option.add_argument( '--headless' ) option.add_argument( '--disable-gpu' ) option.add_argument( "--window-size=1280,1024" ) option.add_argument( "--hide-scrollbars" ) driver = webdriver.Chrome(chrome_options=option) driver. get (url) print (driver.title) scroll_width = driver.execute_script( 'return document.body.parentNode.scrollWidth' ) scroll_height = driver.execute_script( 'return document.body.parentNode.scrollHeight' ) driver.set_window_size(scroll_width, scroll_height) driver.save_screenshot(save_fn) driver.quit() |
Version 3 , 为了把截图放在邮件里直接展示,需要把图片截小一点。然后在邮件内容的HTML中加上一行:${FILE,path="report.html"}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | # -*- coding: utf-8 -*- import time,os from PIL import Image from selenium import webdriver from selenium.webdriver.common.keys import Keys time.sleep( 10 ) url = "http://10.249.4.17/testRailAgent/BillingAnaylisisReport.html?jenkinsJobName=Billing_Office_Live&projectUATName=Billing_Office_UAT&type=1" print url save_fn = "buildNumResult.PNG" option = webdriver.ChromeOptions() option.add_argument( '--headless' ) option.add_argument( '--disable-gpu' ) option.add_argument( "--window-size=1280,1024" ) option.add_argument( "--hide-scrollbars" ) driver = webdriver.Chrome(chrome_options = option) driver.get(url) time.sleep( 30 ) scroll_width = driver.execute_script( 'return document.body.parentNode.scrollWidth' ) scroll_height = driver.execute_script( 'return document.body.parentNode.scrollHeight' ) driver.set_window_size(scroll_width, scroll_height) driver.save_screenshot(save_fn) time.sleep( 5 ) driver.quit() im = Image. open (save_fn) w,h = im.size imgCount = h / 2000 + 1 size = h / imgCount left = 0 shang = 0 index = 0 for i in range (imgCount): i = i + 1 shang + = 1 a = size * left b = size * (i - 1 ) c = w d = size * i region = im.crop((a, b, c, d)) region.save( "report%s.png" % i) fp = open ( "report.html" , "w+b" ) #打开一个文本文件 for i in range ( 1 ,imgCount + 1 ): fp.write( '<img src="report' + str (i) + '.png"></img>' ) #写入数据 fp.close() #关闭文件 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2014-03-19 测试用例Excel模板For Quality Center