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("%s.png" % nowTime)
im = im.crop((left, top, right, bottom))  # 对浏览器截图进行裁剪
        im.save('chandao_img.png')
print("截图完成")
sleep(2)
self.C_driver.close()
if __name__ == '__main__':
run_case().run_chandan()

 

执行结果:

 

截图文件:

 

 

截图内容:

get_screenshot_as_file()

 

save_screenshot()

 

im.crop((left, top, right, bottom)) # 对浏览器截图进行裁剪

 

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。更多文章关注小编公众号:自动化测试 To sharepython

posted @ 2020-10-23 10:47  wen-cheng  阅读(317)  评论(0编辑  收藏  举报