文章目录
Pywinauto+某应用程序(学习至第9讲)–受阻
受阻点
使用pywinauto,chrome legacy window下控件无法定位。因其下为非标准控件,故转战pyautogui。
问题点
1、选择应用程序窗口中,针对窗口类型/窗口标题操作,容易报错。
pywinauto.findbestmatch.MatchError: Could not find 'Chrome_WidgetWin_1' in 'dict_keys(['XXX', 'XXX', 'XXX'])'
目前可选项只有:
dict_keys(['XXX', 'XXX', 'XXX'])
使用正确的窗口标题也无法做到
2、窗口最小化时,无法进行恢复正常大小操作;无法进行指定应用程序打开操作;
1、安装第三方库
1、在线安装
pip install pywinauto
2、离线安装
①下载压缩包并解压
pypi官方网站->pywinauto搜索,下载download files->解压得到底层的文件夹
②为了方便关键,会将tar解压后的文件夹放到python安装目录下,即python的第三方库目录下:D:\python3.6.8\Lib
pywinauto:a set of python modules to automate Microsoft Windows GUI
③cmd进入AA文件夹中,执行python setup.py install 进行安装
2、自动化的切入点–后端技术、程序个数
3、程序辅助检查工具的使用–inspect/spy++
inspect:使用UIA后端解析技术检测使用的控件有哪些。
spy++:使用VS,自动此工具,可以判断是否使用win32后端解析技术来解析控件
两者解析控件数判断,发现Navicat for MYSQL在inspect有更详细的控件展示。并且还可以查看更详细的窗口参数。
4、pywinauto打开指定的应用程序–XXX.exe
from pywinauto.application import Application
# app = Application(backend='uia').start('C:\Program Files\@XXXdesktop\XX.exe'.encode('gb2312').decode('gb2312'))
app = Application(backend='uia').start(r'C:\Program Files\XXXdesktop\XX.exe')
5、pywinauto连接已经打开的应用程序–窗口句柄、进程ID
# 通过进程号连接
app = Application("uia").connect(process=50952)
print(app)
# 通过窗口句柄进行连接,可使用窗口精灵辅助
app = Application("uia").connect(handle=400114)
print(app)
6、选择应用程序的窗口
# 获取指定窗口的所有控件
# 方式一 app[窗口类名/窗口标题]:推荐使用该方式
# 使用类名选择窗口
dlg=app["Chrome_RenderWidgetHostHWND"]
dlg.print_control_identifiers()
# 通过窗口标题去选择窗口
dlg=app["Chrome Legacy Window"]
dlg.print_control_identifiers()
# 方式二 app.窗口类名
dlg=app.Chrome_RenderWidgetHostHWND
dlg.print_control_identifiers()
7、窗口的操作方法
dlg.maximize() # 窗口最大化
dlg.minimize() # 窗口最小化
dlg.restore() # 窗口恢复正常大小
# 查看窗口显示状态:最大化是1:正常是0
status=dlg.get_show_state()
print(status)
# 获取当前窗口显示的坐标
rect=dlg.rectangle()
print(rect) # (L63, T567, R1599, B1399)
dlg.close()
8、窗口的控件选择
app = Application(backend='uia').start(r'C:\Program Files\XXX\XX.exe')
dlg=app["XXX"]
dlg.print_control_identifiers()
程序所有控件
Pane - 'XXX' (L192, T104, R1728, B936)
['XXX', 'XX', 'XXX', 'Pane0', 'Pane1']
child_window(title="XXX", control_type="Pane")
|
| Document - ' ' (L200, T135, R1720, B928)
| [' ', 'Document', ' Document']
| child_window(title=" ", auto_id="88871296", control_type="Document")
|
| Pane - '' (L200, T135, R1720, B928)
| ['Pane2']
|
| TitleBar - '' (L216, T107, R1720, B135)
| ['TitleBar']
| |
| | Menu - '系统' (L200, T112, R222, B134)
| | ['系统Menu', 'Menu', '系统', '系统0', '系统1']
| | child_window(title="系统", auto_id="MenuBar", control_type="MenuBar")
| | |
| | | MenuItem - '系统' (L200, T112, R222, B134)
| | | ['系统MenuItem', 'MenuItem', '系统2']
| | | child_window(title="系统", control_type="MenuItem")
| |
| | Button - '最小化' (L1581, T105, R1628, B135)
| | ['最小化', '最小化Button', 'Button', 'Button0', 'Button1']
| | child_window(title="最小化", control_type="Button")
| |
| | Button - '最大化' (L1628, T105, R1674, B135)
| | ['最大化Button', '最大化', 'Button2']
| | child_window(title="最大化", control_type="Button")
| |
| | Button - '关闭' (L1674, T105, R1721, B135)
| | ['关闭Button', '关闭', 'Button3']
| | child_window(title="关闭", control_type="Button")
|
| Pane - '' (L200, T135, R1720, B928)
| ['Pane3']
| |
| | Pane - '' (L200, T135, R1720, B928)
| | ['Pane4']
| | |
| | | Pane - '' (L200, T135, R1720, B928)
| | | ['Pane5']
| | | |
| | | | Pane - '' (L200, T135, R1720, B928)
| | | | ['Pane6']
| | | | |
| | | | | Pane - '' (L200, T135, R1720, B928)
| | | | | ['Pane7']