Selenium-04-网页自动截图
前言
在自动化测试中,获取屏幕截图是必不可少的,在测试报告中有效的截图能更有说服力。
截图方法
方法1:get_screenshot_as_file()
方法2:save_screenshot()
方法3:im.crop((left, top, right, bottom)) # 对浏览器截图进行裁剪
实例
登录禅道并进行截图
"""
* Create by dell on 2020/10/10
* Author :wencheng
* 微信公众 :自动化测试 To share
"""
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image
from time import sleep
class run_case(object):
def __init__(self):
self.C_driver = webdriver.Chrome()
def run_chandan(self):
self.C_driver.get("http://58.87.103.42:9091/zentao/user-login-L3plbnRhby8=.html")
self.C_driver.maximize_window()
# 截取整个页面
nowTime = time.strftime("%Y%m%d.%H.%M.%S")
t = self.C_driver.get_screenshot_as_file("get_screenshot_as_file%s.png" % nowTime)
print(u"get_screenshot_as_file截图结果: %s" % t)
print("==============================================")
nowTime = time.strftime("%Y%m%d.%H.%M.%S")
t = self.C_driver.save_screenshot("save_screenshot%s.png" % nowTime)
print(u"save_screenshot截图结果: %s" % t)
chandao_img = WebDriverWait(self.C_driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.table-row'))
)
print("==============================================")
nowTime = time.strftime("%Y%m%d.%H.%M.%S")
self.C_driver.save_screenshot("%s.png" % nowTime) # 对整个浏览器页面进行截图
left = chandao_img.location['x']
top = chandao_img.location['y']
right = chandao_img.location['x'] + chandao_img.size['width']
bottom = chandao_img.location['y'] + chandao_img.size['height']
im = Image.open