您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium實(shí)現(xiàn)chrome分屏截圖的合并
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/5/10 13:09:57 ] 推薦標(biāo)簽:功能測試 軟件測試工具

  selenium的截圖功能在chrome下無法實(shí)現(xiàn),但是可以操作滾動(dòng)條來一屏一屏的截圖,然后再合并成一張圖,解決方案如下:
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)):
"""調(diào)整圖片大小
"""
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
# 計(jì)算合成后圖片的寬度(以寬的為準(zhǔn))和高度
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
# 產(chǎn)生一張空白圖
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
  大致這么個(gè)思路

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd