前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium-04-网页自动截图

Selenium-04-网页自动截图

作者头像
wencheng
发布2020-11-03 15:41:21
1.4K0
发布2020-11-03 15:41:21
举报

前言

在自动化测试中,获取屏幕截图是必不可少的,在测试报告中有效的截图能更有说服力。

截图方法

方法1:get_screenshot_as_file()

方法2:save_screenshot()

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

实例

登录禅道并进行截图

代码语言:javascript
复制
"""
 * 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)) # 对浏览器截图进行裁剪

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-10-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 自动化测试 To share 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档