• Python Selenium 自动化实现截屏操作


    一、今天小编就为大家分享一篇对 Python 获取屏幕截图的 3 种方法详解

    1、采用 selenium 中的两种截图方法

    • 方法一: save_screenshot()
    • 方法二: get_screenshot_as_file()
      • 用法一样,都是截取浏览器当前窗口里的内容
    from PIL import ImageGrab
    import time
    
    def screenshot_image1(webdriver, image_path):
        nowTime = time.strftime("%Y-%m-%d_%H-%M-%S")
        imageName = image_path + "/" + "bug_image{}.png".format(nowTime)
        webdriver.save_screenshot(imageName)
    
    def screenshot_image2(webdriver, image_path):
        nowTime = time.strftime("%Y-%m-%d_%H-%M-%S")
        imageName = image_path + "/" + "bug_image{}.png".format(nowTime)
        webdriver.get_screenshot_as_file(imageName)

    2、方法三:Windows 环境下截图需要用到 PIL 库,使用 pip 安装 PIL 库

    • pip install pillow
    • 函数参数介绍如下,默认为全屏截图
    """
    第一个参数 开始截图的x坐标
    第二个参数 开始截图的y坐标
    第三个参数 结束截图的x坐标
    第四个参数 结束截图的y坐标
    """
    bbox = (0, 0, 1920, 1080)
    im = ImageGrab.grab(bbox)   # grab() 默认截全屏
    • 用例输出如下
    import time
    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from screenshot.ScreenShotFunc import *
    
    class Test_Baidu_Search(unittest.TestCase):
    
        def setUp(self) -> None:
            self.driver = webdriver.Chrome()
            self.driver.get("https://www.baidu.com")
            self.driver.maximize_window()
            self.driver.implicitly_wait(10)
    
        def tearDown(self) -> None:
            time.sleep(2)
            self.driver.quit()
    
        def test01(self):
            self.driver.find_element(By.ID, "kw").send_keys("Python")
            time.sleep(2)
            self.driver.find_element(By.ID, "su").click()
            # time.sleep(3)
            title = self.driver.title
            # 调用第一种方法实现截图
            """
            try:
                self.assertIn("Python", title)
            except Exception as E:
                image_path = "D:work_docCodeFiledcs_class6screenshot_image"
                screenshot_image1(self.driver, image_path)
                raise AssertionError(E)
            """
            # 调用第二种方法实现截图
            """
            try:
                self.assertIn("Python", title)
            except Exception as E:
                image_path = "D:work_docCodeFiledcs_class6screenshot_image"
                screenshot_image2(self.driver, image_path)
                raise AssertionError(E)
            """
            # 调用第三种方法实现截图
            try:
                self.assertIn("Python", title)
            except Exception as E:
                image_path = "D:work_docCodeFiledcs_class6screenshot_image"
                screen_shot(image_path)
                raise AssertionError(E)
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    php模拟http请求的方法
    快递100接口开发
    live555从RTSP服务器读取数据到使用接收到的数据流程分析
    VLC源码分析知识总结
    VLC播放器架构剖析
    Android Audio System 之一:AudioTrack如何与AudioFlinger
    VLC各个Module模块之间共享变量的实现方法
    流媒体开发之--HLS--M3U8解析(2): HLS草案
    M3U8格式讲解及实际应用分析
    通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)
  • 原文地址:https://www.cnblogs.com/ZhengYing0813/p/14105755.html
Copyright © 2020-2023  润新知