pywinauto客户端自动化---窗口进行截图

摘要:
做自动化过程中时长会遇到一些截图操作,那么在做客户端的时候,应该怎么进行截图呢?

做自动化过程中时长会遇到一些截图操作,那么在做客户端的时候,应该怎么进行截图呢?

窗口截图

在pywinauto中存在自带的截图函数capture_as_image()

源码:

def capture_as_image(self, rect=None):
        """Return a PIL image of the control.

        See PIL documentation to know what you can do with the resulting
        image.
        """control_rectangle =self.rectangle()
        if not (control_rectangle.width() andcontrol_rectangle.height()):
            returnNone

        #PIL is optional so check first
        if notImageGrab:
            print("PIL does not seem to be installed. "
                  "PIL is required for capture_as_image")
            self.actions.log("PIL does not seem to be installed. "
                             "PIL is required for capture_as_image")
            returnNone

        ifrect:
            control_rectangle =rect

        #get the control rectangle in a way that PIL likes it
        width =control_rectangle.width()
        height =control_rectangle.height()
        left =control_rectangle.left
        right =control_rectangle.right
        top =control_rectangle.top
        bottom =control_rectangle.bottom
        box =(left, top, right, bottom)

        #check the number of monitors connected
        if (sys.platform == 'win32') and (len(win32api.EnumDisplayMonitors()) > 1):
                hwin =win32gui.GetDesktopWindow()
                hwindc =win32gui.GetWindowDC(hwin)
                srcdc =win32ui.CreateDCFromHandle(hwindc)
                memdc =srcdc.CreateCompatibleDC()
                bmp =win32ui.CreateBitmap()
                bmp.CreateCompatibleBitmap(srcdc, width, height)
                memdc.SelectObject(bmp)
                memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

                bmpinfo =bmp.GetInfo()
                bmpstr =bmp.GetBitmapBits(True)
                pil_img_obj = Image.frombuffer('RGB',
                                               (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
                                               bmpstr,
                                               'raw',
                                               'BGRX',
                                               0,
                                               1)
        else:
            #grab the image and get raw data as a string
            pil_img_obj =ImageGrab.grab(box)

        return pil_img_obj

实战

上面的操作方法已经了解了,我们实战进行操作,看看进行如何截图,这里我们截图后,要进行保存,可以根据路径保存或者直接保存在当前目录

#coding:utf-8
from pywinauto importApplication

#打开记事本
app = Application().start(r'C:WindowsSystem32
otepad.exe')
win = app['无标题 - 记事本']
#截图进行保存
a = win.capture_as_image().save('123.png')

pywinauto客户端自动化---窗口进行截图第1张

免责声明:文章转载自《pywinauto客户端自动化---窗口进行截图》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Pycharm下 debug Celery在HTTP通讯过程中,是客户端还是服务端主动断开连接?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

阿里云oss对象存储在vue中的使用

阿里云对象存储OSS(ObjectStorageService)是阿里云提供的海量、安全、低成本、高持久的云存储服务。阿里云oss对象存储是阿里云提供的海量、安全、低成本、高持久的云存储服务,包括服务端加密、客户端加密、防盗链、IP黑白名单、细粒度权限管控、日志审计、WORM特性等。满足企业数据安全与合规要求多线BGP骨干网络。...

微信小程序知识点总结--组件

aspectFill缩放模式保持缩放图像的纵横比,并且只能完全显示图像的短边。也就是说,图片通常只在水平或垂直方向上是完整的,而另一个方向会被截取。...

更改nexus的工作目录

默认情况下,nexus的工作目录位于${user_home}/sonatype工作目录中。在Linux中,如果用户是root用户,则使用/root/sonatype。这便于通过war将nexus安装到servlet容器中,但不利于服务器的集中管理。这需要更改默认的nexus工作目录位置。为了方便管理,您可以选择使用环境变量。...

ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

使用C#生成应用程序以及读取和写入dbfs时,打开方法error[IM002][Microsoft][ODBC驱动程序管理器]中发生错误。找不到数据源名称,也未指定默认驱动程序。这个程序以前使用得很好。升级和修改后,在测试中发现了问题。为了追踪来源,我曾经是一个32位操作系统。现在我安装了一个win764位操作系统。从控制面板到管理工具再到ODBC驱动程序,...

Innodb_large_prefix

但是,索引列的总长度不能超过3072字节的限制仍然存在...