龙空技术网

Python的Windows GUI自动化之Pywinauto(三)

云中随心而记 456

前言:

此时姐妹们对“pythonwindowstab”可能比较注意,咱们都需要了解一些“pythonwindowstab”的相关文章。那么小编在网络上搜集了一些有关“pythonwindowstab””的相关内容,希望同学们能喜欢,你们快快来了解一下吧!

引言:

上一章节中,我们介绍了Pywinauto 是一个用于自动化 Windows 应用程序的 Python 库。它提供了一组功能强大的工具,可以通过控件和窗口句柄来操作和控制 Windows 程序。下面让我们实际操作

一、安装三方包Pywinauto

前提条件:

安装 Python:确保您已在计算机上安装了 Python。您可以从官方网站()上下载最新的 Python 版本,并按照安装向导进行安装。

安装 Microsoft Visual C++:某些 Pywinauto 的依赖库需要 Microsoft Visual C++ 14.0 运行时。您可以从 Microsoft 官方网站()上下载并安装适用于您的操作系统的最新版本。(一般不需要

①联网情况下,电脑 win+R 打开 输入cmd,

然后输入命令 pip install Pywinauto,完成三方包的安装

②若无法连接互联网则去官网下载对应的三方包,然后安装

登录: 然后搜索Pywinauto,下载

下载后将其拷贝到自己的python安装目录 我的是在d盘,然后执行安装命令,注意截图的执行路径,一定要切换到这个路径

二、应用程序实践操作

下面我们用node++来进行实例说明吧,其他window程序同理

2.1 启动应用程序

一般启动应用程序时,此程序可能进程还在,所以一般我都是先杀掉进程再启动

import psutilfrom pywinauto import Applicationfor process in psutil.process_iter():            #杀掉进程    try:        processinfo = process.as_dict(attrs=['pid', 'name'])        if processinfo['name'] == 'notepad++.exe':        #notepad++.exe是进程名,也可以使用进程pid杀掉进程            process.kill()    except psutil.NoSuchProcess:        passpath = r'C:\Program Files (x86)\Notepad++\notepad++.exe'          #应用程序安装的路径app = Application(backend='uia').start(path)   app['Notepad++'].print_control_identifiers()  #获取Notepad++ 界面所有的控件信息#这里为什么启动用Application  前面一章节有解释

执行完毕后就可以看到应用程序已经启动起来

获取控件信息

app['Notepad++'].print_control_identifiers()  #获取Notepad++ 界面所有的控件信息

信息内容打印如下:

Control Identifiers:Dialog - 'new 1 - Notepad++'    (L315, T249, R1655, B908)['new 1 - Notepad++Dialog', 'new 1 - Notepad++', 'Dialog']child_window(title="new 1 - Notepad++", control_type="Window")   |    | Pane - ''    (L325, T351, R1645, B876)   | ['Pane', 'Pane0', 'Pane1']   |    | TabControl - 'Tab'    (L323, T325, R1647, B878)   | ['TabTabControl', 'Tab', 'TabControl']   | child_window(title="Tab", control_type="Tab")   |    |    |    | TabItem - 'new 1'    (L325, T327, R395, B349)   |    | ['TabItem', 'new 1TabItem', 'new 1']   |    | child_window(title="new 1", control_type="TabItem")   |    | StatusBar - ''    (L323, T878, R1647, B900)   | ['Normal text fileStatusBar', 'StatusBar']   |    |    |    | Static - 'Normal text file'    (L323, T880, R907, B900)   |    | ['Static', 'Normal text file', 'Normal text fileStatic', 'Static0', 'Static1']   |    | child_window(title="Normal text file", control_type="Text")   |    |    |    | Static - 'length : 0    lines : 1'    (L909, T880, R1107, B900)   |    | ['Static2', 'length : 0    lines : 1', 'length : 0    lines : 1Static']   |    | child_window(title="length : 0    lines : 1", control_type="Text")   |    |    |    | Static - 'Ln : 1    Col : 1    Sel : 0 | 0'    (L1109, T880, R1367, B900)   |    | ['Static3', 'Ln : 1    Col : 1    Sel : 0 | 0', 'Ln : 1    Col : 1    Sel : 0 | 0Static']   |    | child_window(title="Ln : 1    Col : 1    Sel : 0 | 0", control_type="Text")   |    |    |    | Static - 'Windows (CR LF)'    (L1369, T880, R1477, B900)   |    | ['Static4', 'Windows (CR LF)', 'Windows (CR LF)Static']   |    | child_window(title="Windows (CR LF)", control_type="Text")   |    |    |    | Static - 'UTF-8'    (L1479, T880, R1597, B900)   |    | ['Static5', 'UTF-8', 'UTF-8Static']   |    | child_window(title="UTF-8", control_type="Text")   |    |    |    | Static - 'INS'    (L1599, T880, R1627, B900)   |    | ['Static6', 'INSStatic', 'INS']   |    | child_window(title="INS", control_type="Text")   |    | Pane - ''    (L323, T300, R1647, B325)   | ['Pane2']   |    |    |    | Toolbar - ''    (L325, T300, R1647, B325)   |    | ['Toolbar']   |    |    |    |    |    | Button - ''    (L325, T300, R348, B322)   |    |    | ['Button', 'Button0', 'Button1']   |    |    |    |    |    | Button - ''    (L348, T300, R371, B322)   |    |    | ['Button2']   |    |    |    |    |    | Button - ''    (L371, T300, R394, B322)   |    |    | ['Button3']   |    |    |    |    |    | Button - ''    (L394, T300, R417, B322)   |    |    | ['Button4']   |    |    |    |    |    | Button - ''    (L417, T300, R440, B322)   |    |    | ['Button5']   |    |    |    |    |    | Button - ''    (L440, T300, R463, B322)   |    |    | ['Button6']   |    |    |    |    |    | Button - ''    (L463, T300, R486, B322)   |    |    | ['Button7']   |    |    |    |    |    | Button - ''    (L494, T300, R517, B322)   |    |    | ['Button8']   |    |    |    |    |    | Button - ''    (L517, T300, R540, B322)   |    |    | ['Button9']   |    |    |    |    |    | Button - ''    (L540, T300, R563, B322)   |    |    | ['Button10']   |    |    |    |    |    | Button - ''    (L571, T300, R594, B322)   |    |    | ['Button11']   |    |    |    |    |    | Button - ''    (L594, T300, R617, B322)   |    |    | ['Button12']   |    |    |    |    |    | Button - ''    (L625, T300, R648, B322)   |    |    | ['Button13']   |    |    |    |    |    | Button - ''    (L648, T300, R671, B322)   |    |    | ['Button14']   |    |    |    |    |    | Button - ''    (L679, T300, R702, B322)   |    |    | ['Button15']   |    |    |    |    |    | Button - ''    (L702, T300, R725, B322)   |    |    | ['Button16']   |    |    |    |    |    | Button - ''    (L733, T300, R756, B322)   |    |    | ['Button17']   |    |    |    |    |    | Button - ''    (L756, T300, R779, B322)   |    |    | ['Button18']   |    |    |    |    |    | Button - ''    (L787, T300, R810, B322)   |    |    | ['Button19']   |    |    |    |    |    | Button - ''    (L810, T300, R833, B322)   |    |    | ['Button20']   |    |    |    |    |    | Button - ''    (L833, T300, R856, B322)   |    |    | ['Button21']   |    |    |    |    |    | Button - ''    (L856, T300, R879, B322)   |    |    | ['Button22']   |    |    |    |    |    | Button - ''    (L879, T300, R902, B322)   |    |    | ['Button23']   |    |    |    |    |    | Button - ''    (L902, T300, R925, B322)   |    |    | ['Button24']   |    |    |    |    |    | Button - ''    (L925, T300, R948, B322)   |    |    | ['Button25']   |    |    |    |    |    | Button - ''    (L948, T300, R971, B322)   |    |    | ['Button26']   |    |    |    |    |    | Button - ''    (L979, T300, R1002, B322)   |    |    | ['Button27']   |    |    |    |    |    | Button - ''    (L1002, T300, R1025, B322)   |    |    | ['Button28']   |    |    |    |    |    | Button - ''    (L1025, T300, R1048, B322)   |    |    | ['回放(P)', '回放(P)Button', 'Button29']   |    |    |    |    |    | Button - ''    (L1048, T300, R1071, B322)   |    |    | ['重复运行宏(R)...', '重复运行宏(R)...Button', 'Button30']   |    |    |    |    |    | Button - ''    (L1071, T300, R1094, B322)   |    |    | ['保存录制宏(S)...Button', '保存录制宏(S)...', 'Button31']   |    | TitleBar - ''    (L339, T252, R1647, B280)   | ['TitleBar']   |    |    |    | Menu - '系统'    (L323, T257, R345, B279)   |    | ['系统', '系统Menu', 'Menu', '系统0', '系统1', 'Menu0', 'Menu1']   |    | child_window(title="系统", auto_id="MenuBar", control_type="MenuBar")   |    |    |    |    |    | MenuItem - '系统'    (L323, T257, R345, B279)   |    |    | ['系统2', '系统MenuItem', 'MenuItem', 'MenuItem0', 'MenuItem1']   |    |    | child_window(title="系统", control_type="MenuItem")   |    |    |    | Button - '最小化'    (L1508, T250, R1555, B280)   |    | ['最小化Button', '最小化', 'Button32']   |    | child_window(title="最小化", control_type="Button")   |    |    |    | Button - '最大化'    (L1555, T250, R1601, B280)   |    | ['最大化', '最大化Button', 'Button33']   |    | child_window(title="最大化", control_type="Button")   |    |    |    | Button - '关闭'    (L1601, T250, R1648, B280)   |    | ['关闭Button', '关闭', 'Button34']   |    | child_window(title="关闭", control_type="Button")   |    | Menu - '应用程序'    (L323, T280, R1647, B299)   | ['应用程序', 'Menu2', '应用程序Menu']   | child_window(title="应用程序", auto_id="MenuBar", control_type="MenuBar")   |    |    |    | MenuItem - '文件(F)'    (L323, T280, R375, B299)   |    | ['文件(F)MenuItem', 'MenuItem2', '文件(F)']   |    | child_window(title="文件(F)", control_type="MenuItem")   |    |    |    | MenuItem - '编辑(E)'    (L375, T280, R428, B299)   |    | ['编辑(E)MenuItem', '编辑(E)', 'MenuItem3']   |    | child_window(title="编辑(E)", control_type="MenuItem")   |    |    |    | MenuItem - '搜索(S)'    (L428, T280, R481, B299)   |    | ['搜索(S)MenuItem', '搜索(S)', 'MenuItem4']   |    | child_window(title="搜索(S)", control_type="MenuItem")   |    |    |    | MenuItem - '视图(V)'    (L481, T280, R535, B299)   |    | ['视图(V)', 'MenuItem5', '视图(V)MenuItem']   |    | child_window(title="视图(V)", control_type="MenuItem")   |    |    |    | MenuItem - '编码(N)'    (L535, T280, R591, B299)   |    | ['编码(N)', 'MenuItem6', '编码(N)MenuItem']   |    | child_window(title="编码(N)", control_type="MenuItem")   |    |    |    | MenuItem - '语言(L)'    (L591, T280, R643, B299)   |    | ['语言(L)MenuItem', 'MenuItem7', '语言(L)']   |    | child_window(title="语言(L)", control_type="MenuItem")   |    |    |    | MenuItem - '设置(T)'    (L643, T280, R696, B299)   |    | ['设置(T)MenuItem', '设置(T)', 'MenuItem8']   |    | child_window(title="设置(T)", control_type="MenuItem")   |    |    |    | MenuItem - '工具(O)'    (L696, T280, R752, B299)   |    | ['工具(O)', '工具(O)MenuItem', 'MenuItem9']   |    | child_window(title="工具(O)", control_type="MenuItem")   |    |    |    | MenuItem - '宏(M)'    (L752, T280, R798, B299)   |    | ['宏(M)', '宏(M)MenuItem', 'MenuItem10']   |    | child_window(title="宏(M)", control_type="MenuItem")   |    |    |    | MenuItem - '运行(R)'    (L798, T280, R852, B299)   |    | ['运行(R)', 'MenuItem11', '运行(R)MenuItem']   |    | child_window(title="运行(R)", control_type="MenuItem")   |    |    |    | MenuItem - '插件(P)'    (L852, T280, R905, B299)   |    | ['MenuItem12', '插件(P)', '插件(P)MenuItem']   |    | child_window(title="插件(P)", control_type="MenuItem")   |    |    |    | MenuItem - '窗口(W)'    (L905, T280, R963, B299)   |    | ['窗口(W)MenuItem', '窗口(W)', 'MenuItem13']   |    | child_window(title="窗口(W)", control_type="MenuItem")   |    |    |    | MenuItem - '?'    (L963, T280, R983, B299)   |    | ['?MenuItem', '?', 'MenuItem14']   |    | child_window(title="?", control_type="MenuItem")   |    |    |    | MenuItem - 'X'    (L1625, T280, R1647, B299)   |    | ['MenuItem15', 'XMenuItem', 'X']   |    | child_window(title="X", control_type="MenuItem")Process finished with exit code 0

接下来我们则需要这些控件信息操作应用程序中的控件来实现自动化,大家可以自己将上面代码拷贝操作下

下一章节我们继续讲解,点个关注,不迷路,谢谢大家!

标签: #pythonwindowstab #杀死进程的时候no such process