Selenium实现chrome分屏截图的合并

发表于:2016-5-09 11:23

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:晶莹的营    来源:51Testing软件测试网采编

  selenium的截图功能在chrome下无法实现,但是可以操作滚动条来一屏一屏的截图,然后再合并成一张图,解决方案如下:
def test_capture(url, w_pix,h_pix):
try:
self.driver.set_window_size(w_pix,h_pix)
self.driver.get(url)
self.driver.implicitly_wait(5)
i = 0
img_list = []
while i<30:
js="var q=document.body.scrollTop="+ str(i*wap_pixs[pix][1])+";"
self.driver.execute_script(js)
time.sleep(1)
js1 = "return document.body.scrollHeight-document.body.scrollTop"
s= self.driver.execute_script(js1)
if h_pix >= s:
break
else:
self.driver.save_screenshot(str(i)+'.png')
img_list.append(str(i)+'.png')
i += 1
image_merge(img_list)
except:
print 'error'
from PIL import Image
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
  大致就这么个思路
100家互联网大公司java笔试题汇总,填问卷领取~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号