长图截取方法
*截取长图方法必须是在浏览器无头模式下进行,不然截取不了长图
@allure.step('截长图,图片名-{model}')
def get_image_long(self, url, model=None):
"""
此方法需要重新打开浏览器,并且是无头模式下
:param url:需要截图的链接
:param model:图片名称
:return:None
"""
# 设置chrome开启的模式,headless就是无界面模式
# 一定要使用这个模式,不然截不了全页面,只能截到你电脑的高度
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')
chrome_options.add_argument('window-size=1920x1080')#此处设置分辨率可以自己设置
self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
# 控制浏览器写入并转到链接
self.driver.get(url)
# 接下来是全屏的关键,用js获取页面的宽高,如果有其他需要用js的部分也可以用这个方法
width = self.driver.execute_script("return document.documentElement.scrollWidth")
height = self.driver.execute_script("return document.documentElement.scrollHeight")
# 将浏览器的宽高设置成刚刚获取的宽高
self.driver.set_window_size(width, height)
# 获取本地路径
cur_path = os.path.dirname(os.path.dirname(__file__))
# screenshot_path是存放图片的路径,需要当前总目录下创建一个名为Screenshots的文件夹
now_date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
screenshot_path = os.path.join(cur_path, f'Screenshots\\{now_date}')
if not os.path.exists(screenshot_path):
os.mkdir(screenshot_path)
# 当前时间
date_now = time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))
# 路径
filepath = '{}\\{}.png'.format(screenshot_path, model)
logger.info("截图,图片名为:%s" % model)
# 截图并关掉浏览器
self.driver.get_screenshot_as_file(filepath)
self.driver.close()
短图截取方法
@allure.step('截图,图片名-{model}')
def get_image_short(self, model=None):
# filepath = 指图片保存目录/model(页面功能名称)_当前时间到秒.png
# 截图保存目录
# 拼接日志文件夹,如果不存在则自动创建
cur_path = os.path.dirname(os.path.realpath(__file__))
now_date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
screenshot_path = os.path.join(os.path.dirname(cur_path), f'Screenshots\\{now_date}')
if not os.path.exists(screenshot_path):
os.mkdir(screenshot_path)
# 当前时间
date_now = time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))
# 路径
filepath = '{}\\{}.png'.format(screenshot_path, model, date_now)
# *特此注意:用此方法截图避免了有浏览器自带弹框会遮挡页面导致截图失败,
# *特此注意:self.driver.save_screenshot(filepath)这种方法截图不能截取有浏览器自带弹框的时候,即使关闭了弹框,依旧会报错
# 电脑屏幕左上角坐标和右下角坐标
bbox = (0, 0, 1920, 1080)#此处可根据需要设置参数
# 按照传入的参数截图(坐标可自行设置)
im = ImageGrab.grab(bbox)
# 保存截图到指定位置
im.save(filepath)
logger.info("截图,图片名为:%s" % model)
完整代码
# 截图方法
import os
import allure
from PIL import ImageGrab
from selenium import webdriver
import time
from webdriver_manager.chrome import ChromeDriverManager
from Utils.log import Log
logger = Log()
class Screenshots(object):
def __init__(self, driver):
self.driver = driver
@allure.step('截长图,图片名-{model}')
def get_image_long(self, url, model=None):
"""
此方法需要重新打开浏览器,并且是无头模式下
:param url:
:param model:
:return:
"""
# 设置chrome开启的模式,headless就是无界面模式
# 一定要使用这个模式,不然截不了全页面,只能截到你电脑的高度
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')
chrome_options.add_argument('window-size=1920x1080')
self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
# 控制浏览器写入并转到链接
self.driver.get(url)
# 接下来是全屏的关键,用js获取页面的宽高,如果有其他需要用js的部分也可以用这个方法
width = self.driver.execute_script("return document.documentElement.scrollWidth")
height = self.driver.execute_script("return document.documentElement.scrollHeight")
# 将浏览器的宽高设置成刚刚获取的宽高
self.driver.set_window_size(width, height)
# 获取本地路径
cur_path = os.path.dirname(os.path.dirname(__file__))
# screenshot_path是存放图片的路径
now_date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
screenshot_path = os.path.join(cur_path, f'Screenshots\\{now_date}')
if not os.path.exists(screenshot_path):
os.mkdir(screenshot_path)
# 当前时间
date_now = time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))
# 路径
filepath = '{}\\{}.png'.format(screenshot_path, model)
logger.info("截图,图片名为:%s" % model)
# 截图并关掉浏览器
self.driver.get_screenshot_as_file(filepath)
self.driver.close()
@allure.step('截图,图片名-{model}')
def get_image_short(self, model=None):
# filepath = 指图片保存目录/model(页面功能名称)_当前时间到秒.png
# 截图保存目录
# 拼接日志文件夹,如果不存在则自动创建
cur_path = os.path.dirname(os.path.realpath(__file__))
now_date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
screenshot_path = os.path.join(os.path.dirname(cur_path), f'Screenshots\\{now_date}')
if not os.path.exists(screenshot_path):
os.mkdir(screenshot_path)
# 当前时间
date_now = time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))
# 路径
filepath = '{}\\{}.png'.format(screenshot_path, model, date_now)
# *特此注意:用此方法截图避免了有浏览器自带弹框会遮挡页面导致截图失败,
# *特此注意:self.driver.save_screenshot(filepath)这种方法截图不能截取有浏览器自带弹框的时候,即使关闭了弹框,依旧会报错
# 电脑屏幕左上角坐标和右下角坐标
bbox = (0, 0, 1920, 1080)
# 按照传入的参数截图(坐标可自行设置)
im = ImageGrab.grab(bbox)
# 保存截图到指定位置
im.save(filepath)
logger.info("截图,图片名为:%s" % model)