【pywinauto】简单的UI自动化入门(基于Windows) 第二篇

发布时间:2025-05-07 14:04

参加基础运动培训,入门简单安全 #生活乐趣# #运动乐趣# #运动培训#

这个教程现在来看还是给自己写的,防止以后阿尔兹海默症以后忘了,

所以会不断的引用官方文档和别人的博客,

其实主要还是我怎么写过文档没什么头绪和逻辑,只能根据从上往下的顺序讲了;

其实我在想我这样写文档我干嘛不把中文丢到git上,反正git上太监这么久了

之前写了实例化的窗口怎么连接,连接上以后就可以看看程序了,

首先可以把主要经常用到的窗口定个名字:

from pywinauto import Application

app = Application().start('TestWinForm.exe')

dlg_test = app[r'测试窗体']

这里的写法 dlg_test = app[r‘测试窗体’],看看官方文档:

Once the application instance knows what application it is connected to a dialog to work on needs to be specified.

There are many different ways of doing this. The most common will be using item or attribute access to select a dialog based on it’s title. e.g

一旦应用程序实例知道它连接到哪个应用程序,就可以开始指定要处理的dialog。

有很多方法可以,最常见的还是直接用名字访问:

dlg = app.Notepad

or equivalently  这里上下是等效的

dlg = app['Notepad']

The next easiest method is to ask for the top_window() e.g. 

另一个简单方法就是请求 top_window(),

Note: This is currently fairly untested so I am not sure it will return the correct window. It will definitely be a top level window of the application - it just might not be the one highest in the Z-Order.

作者这里表示这个没怎么测试过但是给你返回的肯定是z-order最高的

If this is not enough control then you can use the same parameters as can be passed to findwindows.find_windows() e.g.

如果都不行建议还是用 findwindows.find_windows()

top_window()能够返回常见的最顶部窗口,但是不是一定的,下面这篇关于z-order结构的介绍文章,写的特别好:

https://blog.csdn.net/baidu_34331290/article/details/88053650

原文地址

下面就是最常用的用法,声明了这个窗口

dlg = app.window(title_re="Page Setup", class_name="#32770")

Finally to have the most control you can use

dialogs = app.windows()

这里看返回的结果

<class 'pywinauto.application.WindowSpecification'>
<pywinauto.application.WindowSpecification object at 0x0550C5F0>

this will return a list of all the visible, enabled, top level windows of the application. You can then use some of the methods in handleprops module select the dialog you want. Once you have the handle you need then use

这里会返回一系列的值,还有程序当前顶层的窗体,如果你想对里面的控件/结构做什么,就需要用到handleprops,

只要你有了句柄你就可以为所欲为

这里其实作者讲了pywinauto的核心理念,

网址:【pywinauto】简单的UI自动化入门(基于Windows) 第二篇 https://www.yuejiaxmz.com/news/view/938235

相关内容

十个比较热门的免费开源桌面自动化应用/框架
python ui 自动化工具 uiautomation
UI自动化测试工具探索:Airtest
案例 | 民生银行基于AI的UI自动化测试实践
GUI测试自动化
深入了解 Bat 脚本:简单而强大的自动化工具——基础版
使用Python实现简单的任务自动化
2024 年 15 个最佳自动化 UI 测试工具【建议收藏】
Windows桌面自动化测试工具:WinAppDriver
【解决方案】智能UI自动化测试

随便看看