selenium实现chrome分屏截图的合并
selenium的截图功能在chrome下无法实现,但是可以操作滚动条来一屏一屏的截图,然后再合并成一张图,合并图片的代码在网上找的,十分感谢那位朋友,具体解决方案如下:直接上代码:
def capture(base_url, pix_w, pix_h, filename):
"""chrome截屏
base_url- 要截屏的url
pix_w- 窗口宽
pix_h- 窗口高
filename-生成截图的文件名
"""
try:
driver = webdriver.Chrome()
driver.set_window_size(pix_w, pix_h+89)
driver.get(base_url)
time.sleep(2)
img_list = []
i = 0
while i<20:
#滚屏
js="var q=document.body.scrollTop="+ str(i*pix_h)+";"
driver.execute_script(js)
js1 = "return document.body.scrollHeight.toString()+','+document.body.scrollTop.toString()"
js1_result = driver.execute_script(js1)
real_scroll_h, real_top = js1_result.split(',')[0], js1_result.split(',')[1]
#real_scroll_h, real_top 是当前滚动条长度和当前滚动条的top,作为是否继续执行的依据,由于存在滚动条向下拉动后会加载新内容的情况,所以需要以下的判断
#如果这次设置的top成功,则继续滚屏
if real_top == str(i*pix_h):
i += 1
driver.save_screenshot('static/capture/wap/wap-'+filename + str(i)+'.png')
img_list.append('static/capture/wap/wap-'+filename + str(i)+'.png')
last_t = real_top
else:
#如果本次设置失败,看这次的top和上一次记录的top值是否相等,相等则说明没有新加载内容,且已到页面底,跳出循环
if real_top != last_t:
last_t = real_top
else:
driver.save_screenshot(filename + str(i+1)+'.png')
img_list.append(filename + str(i+1)+'.png')
break
image_merge(img_list, "", filename+'.png')
except Exception,e:
print e
def image_merge(images, output_dir, output_name='merge.jpg', restriction_max_width=None, restriction_max_height=None):
"""垂直合并多张图片
images - 要合并的图片路径列表
ouput_dir - 输出路径
output_name - 输出文件名
restriction_max_width - 限制合并后的图片最大宽度,如果超过将等比缩小
restriction_max_height - 限制合并后的图片最大高度,如果超过将等比缩小
"""
def image_resize(img, size=(1500, 1100)):
"""调整图片大小
"""
try:
if img.mode not in ('L', 'RGB'):
img = img.convert('RGB')
img = img.resize(size)
except Exception, e:
pass
return img
max_width = 0
total_height = 0
# 计算合成后图片的宽度(以最宽的为准)和高度
for img_path in images:
if os.path.exists(img_path):
img = Image.open(img_path)
width, height = img.size
if width > max_width:
max_width = width
total_height += height # 产生一张空白图
new_img = Image.new('RGB', (max_width, total_height), 255)
# 合并
x = y = 0
for img_path in images:
if os.path.exists(img_path):
img = Image.open(img_path)
width, height = img.size
new_img.paste(img, (x, y))
y += height if restriction_max_width and max_width >= restriction_max_width:
# 如果宽带超过限制
# 等比例缩小
ratio = restriction_max_height / float(max_width)
max_width = restriction_max_width
total_height = int(total_height * ratio)
new_img = image_resize(new_img, size=(max_width, total_height)) if restriction_max_height and total_height >= restriction_max_height:
# 如果高度超过限制
# 等比例缩小
ratio = restriction_max_height / float(total_height)
max_width = int(max_width * ratio)
total_height = restriction_max_height
new_img = image_resize(new_img, size=(max_width, total_height)) if not os.path.exists(output_dir):
os.makedirs(output_dir)
save_path = '%s/%s' % (output_dir, output_name)
new_img.save(save_path)
for img_path in images:
os.remove(img_path)
return save_path
大致就这么个思路
selenium实现chrome分屏截图的合并的更多相关文章
- 【解决Jira】Chrome提示Java插件因过期而遭到阻止(JIRA上传截屏截图)
最近经常被这个问题所困扰:用Chrome访问JIRA上传截屏截图时,地址栏下面弹出通知,提示JAVA插件已过期.但是由于公司要求统一开发环境和设置,不能更新到最新版,就像这样: 结果网页上的Java就 ...
- cat 显示文本、less 分屏显示文本、more 分页显示文件、head 显示文件的前面的内容、cut 切割、paste合并、wc用来对文本进行统计、sort排序、权限、关闭文件、vim的使用
cat 显示文本 -E 显示结尾的$符 -n 对显示的每一行进行编号 -b 对非空行进行编号 -s 对连续的空行进行压缩 tac 倒序显示 less 分屏显示文本 向下翻一屏 空格 向下翻一行 回车 ...
- arcgis api for js入门开发系列六地图分屏对比(含源代码)
上一篇实现了demo的地图标绘模块,本篇新增地图地图分屏对比模块,截图如下(源代码见文章底部): 对效果图的简单介绍一下,在demo只采用了两分屏对比,感兴趣的话,可以在两分屏的基础上拓展,修改css ...
- Appium 截屏截图操作
问题场景:有时当我们的脚本运行报错时,需要通过截屏来分析异常的来源.而selenium也提供了可以截图的方法TakesScreenshot.getScreenshotAs 举例:我们把截屏的图片存储在 ...
- selenium + firefox/chrome/phantomjs登陆之模拟点击
登陆之模拟点击 工具:python/java + selenium + firefox/chrome/phantomjs (1)windows开发环境搭建 默认已经安装好了firefox 安装pip ...
- arcgis api 3.x for js 入门开发系列六地图分屏对比(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- selenium+Headless Chrome实现不弹出浏览器自动化登录
目前由于phantomjs已经不维护了,而新版的Chrome(59+)推出了Headless模式,对爬虫来说尤其是定时任务的爬虫截屏之类的是一大好事. 不过按照网络上的一些方法来写的话,会报下面的错误 ...
- Eclipse 分屏显示同一个代码文件
描述: 今天在使用Eclipse开发的时候不知按错哪个键,出现编辑框分屏显示同一个代码,由于之前没有使用过这一功能,所以就去查了一下,原来是Eclipse的分屏功能. 快捷键: 方式一:Window ...
- 【python爬虫】利用selenium和Chrome浏览器进行自动化网页搜索与浏览
功能简介:利用利用selenium和Chrome浏览器,让其自动打开百度页面,并设置为每页显示50条,接着在百度的搜索框中输入selenium,进行查询.然后再打开的页面中选中“Selenium - ...
随机推荐
- java导入导出Excel文件
package poi.excel; import java.io.IOException; import java.io.InputStream; import java.io.OutputStre ...
- Laravel-admin 表单提交同时验证俩个以上的字段唯一值
$name = isset(request()->all()['name']) ? request()->all()['name'] : ''; $id = isset(request() ...
- GitHub从小白到熟悉<五>
GitHub 主页
- awk--基本操作
二:awk--将一行分为数个字段处理 PS:awk [option] '条件类型 {动作1} 条件类型{动作2} ...' filename PS: awk 'Pattern {action}' fi ...
- LintCode 29---交叉字符串
public class Solution { /** * @param s1: A string * @param s2: A string * @param s3: A string * @ret ...
- 关于redis的几件小事(六)redis的持久化
1.redis持久化的意义 redis持久化的意义,在于 故障恢复 . 如果没有对数据进行持久化,那么如果redis遇到灾难性的故障,就会丢失所有的数据. 如果通过redis的持久化机制将数据持久化到 ...
- OpenSSL(2)创建私有证书颁发机构
如果想要建立自己的CA, OpenSSL已经包含了所有你需要的东西.所有的操作都通过纯命令行执行,虽然不那么友好,整个过程也比较长,但是这可以让你去思考每个细节. 我建议自己创建一套私有的 CA主要是 ...
- c# log4net安装时在AssemblyInfo中提示找不到log4net解决办法
在安装log4net时,按照安装手册需要在AssemblyInfo.cs里添加log4net的配置信息 [assembly: log4net.Config.XmlConfigurator(Config ...
- redis性能指标
1.当内存使用达到设置的最大阀值时,需要选择一种key的回收策略,可在Redis.conf配置文件中修改“maxmemory-policy”属性值. 若是Redis数据集中的key都设置了过期时间,那 ...
- 12、rpm
1.什么是rpm 由红帽开发用于软件包的安装 升级 卸载 查询 2.rpm包是什么样? 组成部分是什么样的? zip-3.0-11.el7.x86_64.rpm #el7 zip-3.0-1. el6 ...