pywinauto-windows app操作

1 winApp

1.1 简单介绍

按理所有手动对windows应用程序都可以用python语言来实现, 本节只介绍一些入门级的基础操作,这块功能,就个人理解而言,觉得比之前的logging、telnet、web的操作都复杂一点,因为windows应用程序本身就比较复杂,较难找到统一的一套标准操作流程。

1.1.1 准备工作

[1].    spyxx.exe:

安装:存放在目录resource\tools\microsoft.spy,安装时默认下一步即可。

说明:一个辅助工具在编写windows应用程序的自动化spy++是一个帮助分析windows窗口的利器,可以查看windows窗口的标题、类名、位置、属性、父子关系、句柄等信息,这些信息可以帮助我们写出能控制windows应用程序的python自动化测试脚本

Note:关于spy++工具的操作方法,见附录B.2

[2].    安装pip

说明:见3 环境搭建

[3].    pywinauton

安装:pip install pywinauto

扫描二维码关注公众号,回复: 11016509 查看本文章

说明:编写widows应用程序的依赖库。

 

[4].    Wireshark

安装:包路径在resource\tools\wireshark-win32-1.2.4.exe

说明:本节中1.2所示的对wireshark操作的软件。不同版本的wireshark的窗口的一些属性变化挺大的,本节的demo脚本只是对wireshark 32位 1.2.4有用,供参考。

1.2 快速入门

Step1:编辑文件suite\case\show_how\alpha_winapp.py,内容如下:

 1 import os
 2 import time
 3 from pywinauto import Application
 4 import win32api
 5 import win32gui
 6 
 7 os.system("taskkill /F /IM  wireshark.exe")
 8 
 9 win32api.WinExec(r"C:\Program Files (x86)\Wireshark\wireshark.exe", 1)  #starting
10 time.sleep(3)
11 
12 topHandle = win32gui.FindWindow("gdkWindowToplevel", "The Wireshark Network Analyzer")  #get handle of top windows
13 
14 topApp = Application(backend='uia').connect(handle=topHandle)   #relate to Application by handle
15 
16 topApp.Dialog.type_keys("^I")    #send ctrl+I to show the avaliable capture interfaces...
17 time.sleep(2)
18 
19 #will show new dialpg window to select interface option.
20 dilagHandle = win32gui.FindWindow("gdkWindowToplevel","Wireshark: Capture Interfaces")
21 dilagApp = Application(backend='uia').connect(handle=dilagHandle)
22 dilagApp.WiresharkCaptureInterfaces.wiresharkexe10.click_input(button="left")   # click the start button of second line
23 
24 time.sleep(10)   #start capture .... 10 seconds.
25 
26 topApp.Dialog.type_keys("^E")   #send ctrl+E to dialg to stop capture
27 topApp.Dialog.type_keys("^+S")  #send ctrl+shift+S to dialg to save capture as file...
28 
29 dilagHandle = win32gui.FindWindow( None, "Wireshark: Save file as")
30 dilagApp = Application(backend='uia').connect(handle=dilagHandle)
31 dilagApp.top_window().child_window(title="文件名(N):", control_type="Edit").set_edit_text(u'C:\\Users\\20002106\Desktop\\temp\\7.pcap')
32 dilagApp.top_window().child_window(title="保存(S)", control_type="Button").click()
33 
34 topApp.Dialog.type_keys("^Q")   #send ctrl+Q to quit capture

Step2:执行Run的快捷键,Alt+Shift+F10

Step3:运行后,会自动打开wireshark,并选择第二接口(因为本人办公PC是双网卡的,第二个接口才是开发项目经常用到的)进行抓报,10秒钟之后,自动点击保存按钮,将文件保存至指定路径下。会在C:\Users\20002106\Desktop\temp下生成文件

1.3 代码解释

1.3.1 导入库文件

在运用pywinauto之前,先导入相关的库文件。

import win32gui
import win32api

from pywinauto.application import Application
from pywinauto.application import WindowSpecification
from pywinauto import actionlogger
from pywinauto import Application

1.3.2 启动应用程序

import win32api

win32api.WinExec(r"C:\Program Files (x86)\Wireshark\wireshark.exe", 1)

1.3.3 获取top窗口

下面这个函数第一个参数是类名,第二个参数是标题名了;返回值是该窗口的控制句柄。

topHandle = win32gui.FindWindow("gdkWindowToplevel", "The Wireshark Network Analyzer")

1.3.4 关联某一个Application

(一)connect

关联一个已经打开的窗口时,可以调用connect函数并传入该窗口的控制句柄。

topApp = Application(backend='uia').connect(handle=topHandle)

该函数的详细解释如下:

    Application.connect(**kwargs):

        """Connect to an already running process

        The action is performed according to only one of parameters

        :param process: a process ID of the target

        :param handle: a window handle of the target

        :param path: a path used to launch the target

        :param timeout: a timeout for process start (relevant if path is specified)

        .. seealso::

           :func:`pywinauto.findwindows.find_elements` - the keyword arguments that

           are also can be used instead of **process**, **handle** or **path**

        """

(二)start

关联一个还没有打开的窗口时,可以调用start函数并传入执行程序的文件路径。

topApp = Application(backend='uia').start(r"C:\Program Files (x86)\Wireshark\Wireshark-gtk.exe")

该函数的详细解释如下:

    Application.start(cmd_line, timeout=None, retry_interval=None,

              create_new_console=False, wait_for_idle=True, work_dir=None):

        """Start the application as specified by cmd_line"""

1.3.5 遍历top窗口

(一)print_control_identifiers

查看该窗口下所有的子窗口的类名、标题、位置、控制类型等信息,可用库WindowSpecification中的函数print_control_identifiers实现。如下是该函数的使用示例:

from pywinauto.application import WindowSpecification

import win32gui

def traverse_func(hwnd):
    criteria = {}
    criteria['backend'] = "uia"
   
criteria['handle'] = hwnd
    WindowSpecification(criteria).print_control_identifiers()

#获取top窗口句柄
topHandle = win32gui.FindWindow("gdkWindowToplevel", "The Wireshark Network Analyzer")
#遍历输出该窗口及其所有子窗口下的信息,如类名、标题、位置、控制类型等
traverse_func(topHandle)

上面code执行后,会在console端输出如下

  1 C:\Users\20002106\AppData\Local\Programs\Python\Python36\python.exe "D:/auto test/Together_v0.2/suite/case/show_how/debug.py"
  2 Control Identifiers:
  3 
  4 Dialog - 'The Wireshark Network Analyzer'    (L266, T192, R757, B702)
  5 ['Dialog', 'The Wireshark Network Analyzer', 'The Wireshark Network AnalyzerDialog']
  6 child_window(title="The Wireshark Network Analyzer", control_type="Window")
  7    | 
  8    | Pane - 'wireshark.exe'    (L673, T672, R678, B693)
  9    | ['wireshark.exe', 'wireshark.exePane', 'Pane', 'wireshark.exe0', 'wireshark.exe1', 'wireshark.exePane0', 'wireshark.exePane1', 'Pane0', 'Pane1']
 10    | child_window(title="wireshark.exe", control_type="Pane")
 11    | 
 12    | Pane - 'wireshark.exe'    (L540, T672, R545, B693)
 13    | ['wireshark.exe2', 'wireshark.exePane2', 'Pane2']
 14    | child_window(title="wireshark.exe", control_type="Pane")
 15    | 
 16    | Pane - 'wireshark.exe'    (L691, T249, R717, B275)
 17    | ['wireshark.exe3', 'wireshark.exePane3', 'Pane3']
 18    | child_window(title="wireshark.exe", control_type="Pane")
 19    | 
 20    | Pane - 'wireshark.exe'    (L693, T282, R718, B307)
 21    | ['wireshark.exe4', 'wireshark.exePane4', 'Pane4']
 22    | child_window(title="wireshark.exe", control_type="Pane")
 23    | 
 24    | Pane - 'wireshark.exe'    (L318, T282, R693, B307)
 25    | ['wireshark.exe5', 'wireshark.exePane5', 'Pane5']
 26    | child_window(title="wireshark.exe", control_type="Pane")
 27    |    | 
 28    |    | Pane - 'wireshark.exe'    (L320, T284, R691, B305)
 29    |    | ['wireshark.exe6', 'wireshark.exePane6', 'Pane6']
 30    |    | child_window(title="wireshark.exe", control_type="Pane")
 31    | 
 32    | Pane - 'wireshark.exe'    (L665, T249, R691, B275)
 33    | ['wireshark.exe7', 'wireshark.exePane7', 'Pane7']
 34    | child_window(title="wireshark.exe", control_type="Pane")
 35    | 
 36    | Pane - 'wireshark.exe'    (L639, T249, R665, B275)
 37    | ['wireshark.exe8', 'wireshark.exePane8', 'Pane8']
 38    | child_window(title="wireshark.exe", control_type="Pane")
 39    | 
 40    | Pane - 'wireshark.exe'    (L613, T249, R639, B275)
 41    | ['wireshark.exe9', 'wireshark.exePane9', 'Pane9']
 42    | child_window(title="wireshark.exe", control_type="Pane")
 43    | 
 44    | Pane - 'wireshark.exe'    (L731, T312, R748, B653)
 45    | ['wireshark.exe10', 'wireshark.exePane10', 'Pane10']
 46    | child_window(title="wireshark.exe", control_type="Pane")
 47    | 
 48    | Pane - 'wireshark.exe'    (L678, T672, R748, B693)
 49    | ['wireshark.exe11', 'wireshark.exePane11', 'Pane11']
 50    | child_window(title="wireshark.exe", control_type="Pane")
 51    |    | 
 52    |    | Pane - 'wireshark.exe'    (L732, T675, R750, B693)
 53    |    | ['wireshark.exe12', 'wireshark.exePane12', 'Pane12']
 54    |    | child_window(title="wireshark.exe", control_type="Pane")
 55    | 
 56    | Pane - 'wireshark.exe'    (L275, T672, R289, B693)
 57    | ['wireshark.exe13', 'wireshark.exePane13', 'Pane13']
 58    | child_window(title="wireshark.exe", control_type="Pane")
 59    | 
 60    | Pane - 'wireshark.exe'    (L275, T654, R730, B671)
 61    | ['wireshark.exe14', 'wireshark.exePane14', 'Pane14']
 62    | child_window(title="wireshark.exe", control_type="Pane")
 63    | 
 64    | Pane - 'wireshark.exe'    (L275, T312, R730, B653)
 65    | ['wireshark.exe15', 'wireshark.exePane15', 'Pane15']
 66    | child_window(title="wireshark.exe", control_type="Pane")
 67    |    | 
 68    |    | Pane - 'wireshark.exe'    (L277, T314, R728, B651)
 69    |    | ['wireshark.exe16', 'wireshark.exePane16', 'Pane16']
 70    |    | child_window(title="wireshark.exe", control_type="Pane")
 71    |    |    | 
 72    |    |    | Pane - 'wireshark.exe'    (L277, T314, R1654, B859)
 73    |    |    | ['wireshark.exe17', 'wireshark.exePane17', 'Pane17']
 74    |    |    | child_window(title="wireshark.exe", control_type="Pane")
 75    |    |    |    | 
 76    |    |    |    | Pane - 'wireshark.exe'    (L1364, T408, R1644, B849)
 77    |    |    |    | ['wireshark.exe18', 'wireshark.exePane18', 'Pane18']
 78    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
 79    |    |    |    |    | 
 80    |    |    |    |    | Pane - 'wireshark.exe'    (L1374, T571, R1634, B618)
 81    |    |    |    |    | ['wireshark.exe19', 'wireshark.exePane19', 'Pane19']
 82    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
 83    |    |    |    |    | 
 84    |    |    |    |    | Pane - 'wireshark.exe'    (L1374, T509, R1634, B556)
 85    |    |    |    |    | ['wireshark.exe20', 'wireshark.exePane20', 'Pane20']
 86    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
 87    |    |    |    |    | 
 88    |    |    |    |    | Pane - 'wireshark.exe'    (L1374, T447, R1634, B494)
 89    |    |    |    |    | ['wireshark.exe21', 'wireshark.exePane21', 'Pane21']
 90    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
 91    |    |    |    |    | 
 92    |    |    |    |    | Pane - 'wireshark.exe'    (L1364, T408, R1644, B432)
 93    |    |    |    |    | ['wireshark.exe22', 'wireshark.exePane22', 'Pane22']
 94    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
 95    |    |    |    | 
 96    |    |    |    | Pane - 'wireshark.exe'    (L649, T408, R1354, B849)
 97    |    |    |    | ['wireshark.exe23', 'wireshark.exePane23', 'Pane23']
 98    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
 99    |    |    |    |    | 
100    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T765, R1344, B812)
101    |    |    |    |    | ['wireshark.exe24', 'wireshark.exePane24', 'Pane24']
102    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
103    |    |    |    |    | 
104    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T734, R1344, B755)
105    |    |    |    |    | ['wireshark.exe25', 'wireshark.exePane25', 'Pane25']
106    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
107    |    |    |    |    | 
108    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T712, R1344, B733)
109    |    |    |    |    | ['wireshark.exe26', 'wireshark.exePane26', 'Pane26']
110    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
111    |    |    |    |    | 
112    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T690, R1344, B711)
113    |    |    |    |    | ['wireshark.exe27', 'wireshark.exePane27', 'Pane27']
114    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
115    |    |    |    |    | 
116    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T668, R1344, B689)
117    |    |    |    |    | ['wireshark.exe28', 'wireshark.exePane28', 'Pane28']
118    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
119    |    |    |    |    | 
120    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T646, R1344, B667)
121    |    |    |    |    | ['wireshark.exe29', 'wireshark.exePane29', 'Pane29']
122    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
123    |    |    |    |    | 
124    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T624, R1344, B645)
125    |    |    |    |    | ['wireshark.exe30', 'wireshark.exePane30', 'Pane30']
126    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
127    |    |    |    |    | 
128    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T602, R1344, B623)
129    |    |    |    |    | ['wireshark.exe31', 'wireshark.exePane31', 'Pane31']
130    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
131    |    |    |    |    | 
132    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T580, R1344, B601)
133    |    |    |    |    | ['wireshark.exe32', 'wireshark.exePane32', 'Pane32']
134    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
135    |    |    |    |    | 
136    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T558, R1344, B579)
137    |    |    |    |    | ['wireshark.exe33', 'wireshark.exePane33', 'Pane33']
138    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
139    |    |    |    |    | 
140    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T536, R1344, B557)
141    |    |    |    |    | ['wireshark.exe34', 'wireshark.exePane34', 'Pane34']
142    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
143    |    |    |    |    | 
144    |    |    |    |    | Pane - 'wireshark.exe'    (L659, T447, R1344, B494)
145    |    |    |    |    | ['wireshark.exe35', 'wireshark.exePane35', 'Pane35']
146    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
147    |    |    |    |    | 
148    |    |    |    |    | Pane - 'wireshark.exe'    (L649, T408, R1354, B432)
149    |    |    |    |    | ['wireshark.exe36', 'wireshark.exePane36', 'Pane36']
150    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
151    |    |    |    | 
152    |    |    |    | Pane - 'wireshark.exe'    (L287, T660, R639, B849)
153    |    |    |    | ['wireshark.exe37', 'wireshark.exePane37', 'Pane37']
154    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
155    |    |    |    |    | 
156    |    |    |    |    | Pane - 'wireshark.exe'    (L297, T761, R629, B808)
157    |    |    |    |    | ['wireshark.exe38', 'wireshark.exePane38', 'Pane38']
158    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
159    |    |    |    |    | 
160    |    |    |    |    | Pane - 'wireshark.exe'    (L297, T699, R629, B746)
161    |    |    |    |    | ['wireshark.exe39', 'wireshark.exePane39', 'Pane39']
162    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
163    |    |    |    |    | 
164    |    |    |    |    | Pane - 'wireshark.exe'    (L287, T660, R639, B684)
165    |    |    |    |    | ['wireshark.exe40', 'wireshark.exePane40', 'Pane40']
166    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
167    |    |    |    | 
168    |    |    |    | Pane - 'wireshark.exe'    (L287, T408, R639, B650)
169    |    |    |    | ['wireshark.exe41', 'wireshark.exePane41', 'Pane41']
170    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
171    |    |    |    |    | 
172    |    |    |    |    | Pane - 'wireshark.exe'    (L297, T584, R629, B631)
173    |    |    |    |    | ['wireshark.exe42', 'wireshark.exePane42', 'Pane42']
174    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
175    |    |    |    |    | 
176    |    |    |    |    | Pane - 'wireshark.exe'    (L297, T556, R629, B573)
177    |    |    |    |    | ['wireshark.exe43', 'wireshark.exePane43', 'Pane43']
178    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
179    |    |    |    |    | 
180    |    |    |    |    | Pane - 'wireshark.exe'    (L297, T537, R629, B554)
181    |    |    |    |    | ['wireshark.exe44', 'wireshark.exePane44', 'Pane44']
182    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
183    |    |    |    |    | 
184    |    |    |    |    | Pane - 'wireshark.exe'    (L297, T447, R629, B494)
185    |    |    |    |    | ['wireshark.exe45', 'wireshark.exePane45', 'Pane45']
186    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
187    |    |    |    |    | 
188    |    |    |    |    | Pane - 'wireshark.exe'    (L287, T408, R639, B432)
189    |    |    |    |    | ['wireshark.exe46', 'wireshark.exePane46', 'Pane46']
190    |    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
191    |    |    |    | 
192    |    |    |    | Pane - 'wireshark.exe'    (L277, T314, R1654, B398)
193    |    |    |    | ['wireshark.exe47', 'wireshark.exePane47', 'Pane47']
194    |    |    |    | child_window(title="wireshark.exe", control_type="Pane")
195    | 
196    | Pane - 'wireshark.exe'    (L277, T281, R318, B308)
197    | ['wireshark.exe48', 'wireshark.exePane48', 'Pane48']
198    | child_window(title="wireshark.exe", control_type="Pane")
199    | 
200    | Pane - 'wireshark.exe'    (L721, T249, R746, B275)
201    | ['wireshark.exe49', 'wireshark.exePane49', 'Pane49']
202    | child_window(title="wireshark.exe", control_type="Pane")
203    | 
204    | Pane - 'wireshark.exe'    (L587, T249, R613, B275)
205    | ['wireshark.exe50', 'wireshark.exePane50', 'Pane50']
206    | child_window(title="wireshark.exe", control_type="Pane")
207    | 
208    | Pane - 'wireshark.exe'    (L561, T249, R587, B275)
209    | ['wireshark.exe51', 'wireshark.exePane51', 'Pane51']
210    | child_window(title="wireshark.exe", control_type="Pane")
211    | 
212    | Pane - 'wireshark.exe'    (L523, T249, R549, B275)
213    | ['wireshark.exe52', 'wireshark.exePane52', 'Pane52']
214    | child_window(title="wireshark.exe", control_type="Pane")
215    | 
216    | Pane - 'wireshark.exe'    (L497, T249, R523, B275)
217    | ['wireshark.exe53', 'wireshark.exePane53', 'Pane53']
218    | child_window(title="wireshark.exe", control_type="Pane")
219    | 
220    | Pane - 'wireshark.exe'    (L471, T249, R497, B275)
221    | ['wireshark.exe54', 'wireshark.exePane54', 'Pane54']
222    | child_window(title="wireshark.exe", control_type="Pane")
223    | 
224    | Pane - 'wireshark.exe'    (L445, T249, R471, B275)
225    | ['wireshark.exe55', 'wireshark.exePane55', 'Pane55']
226    | child_window(title="wireshark.exe", control_type="Pane")
227    | 
228    | Pane - 'wireshark.exe'    (L419, T249, R445, B275)
229    | ['wireshark.exe56', 'wireshark.exePane56', 'Pane56']
230    | child_window(title="wireshark.exe", control_type="Pane")
231    | 
232    | Pane - 'wireshark.exe'    (L381, T249, R407, B275)
233    | ['wireshark.exe57', 'wireshark.exePane57', 'Pane57']
234    | child_window(title="wireshark.exe", control_type="Pane")
235    | 
236    | Pane - 'wireshark.exe'    (L355, T249, R381, B275)
237    | ['wireshark.exe58', 'wireshark.exePane58', 'Pane58']
238    | child_window(title="wireshark.exe", control_type="Pane")
239    | 
240    | Pane - 'wireshark.exe'    (L329, T249, R355, B275)
241    | ['wireshark.exe59', 'wireshark.exePane59', 'Pane59']
242    | child_window(title="wireshark.exe", control_type="Pane")
243    | 
244    | Pane - 'wireshark.exe'    (L303, T249, R329, B275)
245    | ['wireshark.exe60', 'wireshark.exePane60', 'Pane60']
246    | child_window(title="wireshark.exe", control_type="Pane")
247    | 
248    | Pane - 'wireshark.exe'    (L277, T249, R303, B275)
249    | ['wireshark.exe61', 'wireshark.exePane61', 'Pane61']
250    | child_window(title="wireshark.exe", control_type="Pane")
251    | 
252    | Pane - 'wireshark.exe'    (L275, T223, R748, B246)
253    | ['wireshark.exe62', 'wireshark.exePane62', 'Pane62']
254    | child_window(title="wireshark.exe", control_type="Pane")
255    |    | 
256    |    | Pane - 'wireshark.exe'    (L719, T223, R760, B246)
257    |    | ['wireshark.exe63', 'wireshark.exePane63', 'Pane63']
258    |    | child_window(title="wireshark.exe", control_type="Pane")
259    |    | 
260    |    | Pane - 'wireshark.exe'    (L673, T223, R719, B246)
261    |    | ['wireshark.exe64', 'wireshark.exePane64', 'Pane64']
262    |    | child_window(title="wireshark.exe", control_type="Pane")
263    |    | 
264    |    | Pane - 'wireshark.exe'    (L599, T223, R673, B246)
265    |    | ['wireshark.exe65', 'wireshark.exePane65', 'Pane65']
266    |    | child_window(title="wireshark.exe", control_type="Pane")
267    |    | 
268    |    | Pane - 'wireshark.exe'    (L535, T223, R599, B246)
269    |    | ['wireshark.exe66', 'wireshark.exePane66', 'Pane66']
270    |    | child_window(title="wireshark.exe", control_type="Pane")
271    |    | 
272    |    | Pane - 'wireshark.exe'    (L477, T223, R535, B246)
273    |    | ['wireshark.exe67', 'wireshark.exePane67', 'Pane67']
274    |    | child_window(title="wireshark.exe", control_type="Pane")
275    |    | 
276    |    | Pane - 'wireshark.exe'    (L417, T223, R477, B246)
277    |    | ['wireshark.exe68', 'wireshark.exePane68', 'Pane68']
278    |    | child_window(title="wireshark.exe", control_type="Pane")
279    |    | 
280    |    | Pane - 'wireshark.exe'    (L386, T223, R417, B246)
281    |    | ['wireshark.exe69', 'wireshark.exePane69', 'Pane69']
282    |    | child_window(title="wireshark.exe", control_type="Pane")
283    |    | 
284    |    | Pane - 'wireshark.exe'    (L345, T223, R386, B246)
285    |    | ['wireshark.exe70', 'wireshark.exePane70', 'Pane70']
286    |    | child_window(title="wireshark.exe", control_type="Pane")
287    |    | 
288    |    | Pane - 'wireshark.exe'    (L309, T223, R345, B246)
289    |    | ['wireshark.exe71', 'wireshark.exePane71', 'Pane71']
290    |    | child_window(title="wireshark.exe", control_type="Pane")
291    |    | 
292    |    | Pane - 'wireshark.exe'    (L276, T223, R309, B246)
293    |    | ['wireshark.exe72', 'wireshark.exePane72', 'Pane72']
294    |    | child_window(title="wireshark.exe", control_type="Pane")
295    | 
296    | Pane - 'wireshark.exe'    (L275, T247, R748, B277)
297    | ['wireshark.exe73', 'wireshark.exePane73', 'Pane73']
298    | child_window(title="wireshark.exe", control_type="Pane")
299    | 
300    | Pane - 'wireshark.exe'    (L275, T279, R748, B310)
301    | ['wireshark.exe74', 'wireshark.exePane74', 'Pane74']
302    | child_window(title="wireshark.exe", control_type="Pane")
303    | 
304    | TitleBar - 'None'    (L290, T195, R749, B222)
305    | ['', 'TitleBar']
306    |    | 
307    |    | Menu - '系统'    (L274, T200, R295, B221)
308    |    | ['Menu', '系统', '系统Menu', '系统0', '系统1']
309    |    | child_window(title="系统", control_type="MenuBar")
310    |    |    | 
311    |    |    | MenuItem - '系统'    (L274, T200, R295, B221)
312    |    |    | ['系统2', '系统MenuItem', 'MenuItem']
313    |    |    | child_window(title="系统", control_type="MenuItem")
314    |    | 
315    |    | Button - '最小化'    (L646, T193, R675, B213)
316    |    | ['最小化Button', 'Button', '最小化', 'Button0', 'Button1']
317    |    | child_window(title="最小化", control_type="Button")
318    |    | 
319    |    | Button - '最大化'    (L675, T193, R702, B213)
320    |    | ['最大化Button', 'Button2', '最大化']
321    |    | child_window(title="最大化", control_type="Button")
322    |    | 
323    |    | Button - '关闭'    (L702, T193, R751, B213)
324    |    | ['关闭', 'Button3', '关闭Button']
325    |    | child_window(title="关闭", control_type="Button")
326 
327 Process finished with exit code 0
top窗口遍历

函数print_control_identifiers的详细解释如下:

    WindowSpecification.print_control_identifiers(depth=None, filename=None):

        """

        Prints the 'identifiers'

        Prints identifiers for the control and for its descendants to

        a depth of **depth** (the whole subtree if **None**).

        .. note:: The identifiers printed by this method have been made

               unique. So if you have 2 edit boxes, they won't both have "Edit"

               listed in their identifiers. In fact the first one can be

               referred to as "Edit", "Edit0", "Edit1" and the 2nd should be

               referred to as "Edit2".

        """

(二)win32gui

该窗口及其所有子窗口下的控制句柄、标题和类名. 如下这段code是从Simon Brunning的个人网站上得来的。

 1 # Programmer : Simon Brunning - [email protected]
 2 # Date       : 25 June 2003
 3 # Copyright  : Released to the public domain. Provided as-is, with no warranty.
 4 import win32gui
 5 def _windowEnumerationHandler(hwnd, resultList):
 6     '''Pass to win32gui.EnumWindows() to generate list of window handle,
 7     window text, window class tuples.'''
 8     resultList.append((hwnd,
 9                        win32gui.GetWindowText(hwnd),
10                        win32gui.GetClassName(hwnd)))
11 
12 def dumpWindow(hwnd):
13     '''Dump all controls from a window into a nested list
14     Useful during development, allowing to you discover the structure of the
15     contents of a window, showing the text and class of all contained controls.
16 
17     Arguments:      The window handle of the top level window to dump.
18 
19     Returns         A nested list of controls. Each entry consists of the
20                     control's hwnd, its text, its class, and its sub-controls,
21                     if any.
22 
23     Usage example:  replaceDialog = findTopWindow(wantedText='Replace')
24                     pprint.pprint(dumpWindow(replaceDialog))
25     '''
26     windows = []
27     try:
28         win32gui.EnumChildWindows(hwnd,_windowEnumerationHandler, windows)
29     except win32gui.error:
30         # No child windows
31         return
32     windows = [list(window) for window in windows]
33     for window in windows:
34         childHwnd, windowText, windowClass = window
35         window_content = dumpWindow(childHwnd)
36         if window_content:
37             window.append(window_content)
38     return windows
39 
40 
41 #获取top窗口句柄
42 topHandle = win32gui.FindWindow("gdkWindowToplevel", "The Wireshark Network Analyzer")
43 
44 res = dumpWindow(topHandle)
45 print(res)

执行上面code之后,会在console端输出如下信息:

1 C:\Users\20002106\AppData\Local\Programs\Python\Python36\python.exe "D:/auto test/Together_v0.2/suite/case/show_how/debug.py"
2 [[1183342, 'wireshark.exe', 'gdkWindowChild'], [4064782, 'wireshark.exe', 'gdkWindowChild'], [22875536, 'wireshark.exe', 'gdkWindowChild'], [43781728, 'wireshark.exe', 'gdkWindowChild'], [55709570, 'wireshark.exe', 'gdkWindowChild', [[22351234, 'wireshark.exe', 'gdkWindowChild']]], [22351234, 'wireshark.exe', 'gdkWindowChild'], [9506312, 'wireshark.exe', 'gdkWindowChild'], [3345970, 'wireshark.exe', 'gdkWindowChild'], [19992220, 'wireshark.exe', 'gdkWindowChild'], [55315584, 'wireshark.exe', 'gdkWindowChild'], [13569536, 'wireshark.exe', 'gdkWindowChild', [[14356024, 'wireshark.exe', 'gdkWindowChild']]], [14356024, 'wireshark.exe', 'gdkWindowChild'], [16319934, 'wireshark.exe', 'gdkWindowChild'], [8064598, 'wireshark.exe', 'gdkWindowChild'], [17173902, 'wireshark.exe', 'gdkWindowChild', [[1838544, 'wireshark.exe', 'gdkWindowChild', [[6819324, 'wireshark.exe', 'gdkWindowChild', [[3018238, 'wireshark.exe', 'gdkWindowChild', [[18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild']]], [18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild'], [1641994, 'wireshark.exe', 'gdkWindowChild', [[36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild']]], [36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild'], [58330526, 'wireshark.exe', 'gdkWindowChild', [[21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild']]], [21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild'], [26151942, 'wireshark.exe', 'gdkWindowChild', [[48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild']]], [48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild'], [49614360, 'wireshark.exe', 'gdkWindowChild']]], [3018238, 'wireshark.exe', 'gdkWindowChild', [[18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild']]], [18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild'], [1641994, 'wireshark.exe', 'gdkWindowChild', [[36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild']]], [36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild'], [58330526, 'wireshark.exe', 'gdkWindowChild', [[21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild']]], [21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild'], [26151942, 'wireshark.exe', 'gdkWindowChild', [[48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild']]], [48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild'], [49614360, 'wireshark.exe', 'gdkWindowChild']]], [6819324, 'wireshark.exe', 'gdkWindowChild', [[3018238, 'wireshark.exe', 'gdkWindowChild', [[18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild']]], [18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild'], [1641994, 'wireshark.exe', 'gdkWindowChild', [[36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild']]], [36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild'], [58330526, 'wireshark.exe', 'gdkWindowChild', [[21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild']]], [21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild'], [26151942, 'wireshark.exe', 'gdkWindowChild', [[48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild']]], [48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild'], [49614360, 'wireshark.exe', 'gdkWindowChild']]], [3018238, 'wireshark.exe', 'gdkWindowChild', [[18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild']]], [18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild'], [1641994, 'wireshark.exe', 'gdkWindowChild', [[36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild']]], [36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild'], [58330526, 'wireshark.exe', 'gdkWindowChild', [[21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild']]], [21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild'], [26151942, 'wireshark.exe', 'gdkWindowChild', [[48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild']]], [48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild'], [49614360, 'wireshark.exe', 'gdkWindowChild']]], [1838544, 'wireshark.exe', 'gdkWindowChild', [[6819324, 'wireshark.exe', 'gdkWindowChild', [[3018238, 'wireshark.exe', 'gdkWindowChild', [[18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild']]], [18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild'], [1641994, 'wireshark.exe', 'gdkWindowChild', [[36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild']]], [36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild'], [58330526, 'wireshark.exe', 'gdkWindowChild', [[21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild']]], [21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild'], [26151942, 'wireshark.exe', 'gdkWindowChild', [[48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild']]], [48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild'], [49614360, 'wireshark.exe', 'gdkWindowChild']]], [3018238, 'wireshark.exe', 'gdkWindowChild', [[18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild']]], [18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild'], [1641994, 'wireshark.exe', 'gdkWindowChild', [[36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild']]], [36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild'], [58330526, 'wireshark.exe', 'gdkWindowChild', [[21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild']]], [21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild'], [26151942, 'wireshark.exe', 'gdkWindowChild', [[48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild']]], [48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild'], [49614360, 'wireshark.exe', 'gdkWindowChild']]], [6819324, 'wireshark.exe', 'gdkWindowChild', [[3018238, 'wireshark.exe', 'gdkWindowChild', [[18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild']]], [18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild'], [1641994, 'wireshark.exe', 'gdkWindowChild', [[36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild']]], [36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild'], [58330526, 'wireshark.exe', 'gdkWindowChild', [[21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild']]], [21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild'], [26151942, 'wireshark.exe', 'gdkWindowChild', [[48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild']]], [48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild'], [49614360, 'wireshark.exe', 'gdkWindowChild']]], [3018238, 'wireshark.exe', 'gdkWindowChild', [[18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild']]], [18746872, 'wireshark.exe', 'gdkWindowChild'], [4326734, 'wireshark.exe', 'gdkWindowChild'], [65538986, 'wireshark.exe', 'gdkWindowChild'], [30870278, 'wireshark.exe', 'gdkWindowChild'], [1641994, 'wireshark.exe', 'gdkWindowChild', [[36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild']]], [36704044, 'wireshark.exe', 'gdkWindowChild'], [22875690, 'wireshark.exe', 'gdkWindowChild'], [19074528, 'wireshark.exe', 'gdkWindowChild'], [397030, 'wireshark.exe', 'gdkWindowChild'], [109383190, 'wireshark.exe', 'gdkWindowChild'], [6556986, 'wireshark.exe', 'gdkWindowChild'], [24382370, 'wireshark.exe', 'gdkWindowChild'], [17892828, 'wireshark.exe', 'gdkWindowChild'], [2493690, 'wireshark.exe', 'gdkWindowChild'], [49548828, 'wireshark.exe', 'gdkWindowChild'], [48696318, 'wireshark.exe', 'gdkWindowChild'], [43846428, 'wireshark.exe', 'gdkWindowChild'], [21367960, 'wireshark.exe', 'gdkWindowChild'], [58330526, 'wireshark.exe', 'gdkWindowChild', [[21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild']]], [21761542, 'wireshark.exe', 'gdkWindowChild'], [2166378, 'wireshark.exe', 'gdkWindowChild'], [2624904, 'wireshark.exe', 'gdkWindowChild'], [26151942, 'wireshark.exe', 'gdkWindowChild', [[48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild']]], [48696230, 'wireshark.exe', 'gdkWindowChild'], [9047492, 'wireshark.exe', 'gdkWindowChild'], [14945600, 'wireshark.exe', 'gdkWindowChild'], [5114712, 'wireshark.exe', 'gdkWindowChild'], [28707614, 'wireshark.exe', 'gdkWindowChild'], [49614360, 'wireshark.exe', 'gdkWindowChild'], [31918884, 'wireshark.exe', 'gdkWindowChild'], [4591400, 'wireshark.exe', 'gdkWindowChild'], [17173144, 'wireshark.exe', 'gdkWindowChild'], [3215228, 'wireshark.exe', 'gdkWindowChild'], [5246772, 'wireshark.exe', 'gdkWindowChild'], [921462, 'wireshark.exe', 'gdkWindowChild'], [15273674, 'wireshark.exe', 'gdkWindowChild'], [1707718, 'wireshark.exe', 'gdkWindowChild'], [23793234, 'wireshark.exe', 'gdkWindowChild'], [32903044, 'wireshark.exe', 'gdkWindowChild'], [49024548, 'wireshark.exe', 'gdkWindowChild'], [47779426, 'wireshark.exe', 'gdkWindowChild'], [7081604, 'wireshark.exe', 'gdkWindowChild'], [18222932, 'wireshark.exe', 'gdkWindowChild'], [1183476, 'wireshark.exe', 'gdkWindowChild'], [46272020, 'wireshark.exe', 'gdkWindowChild'], [19468152, 'wireshark.exe', 'gdkWindowChild'], [50400846, 'wireshark.exe', 'gdkWindowChild'], [32509060, 'wireshark.exe', 'gdkWindowChild'], [6622164, 'wireshark.exe', 'gdkWindowChild'], [2296898, 'wireshark.exe', 'gdkWindowChild'], [11144866, 'wireshark.exe', 'gdkWindowChild'], [5246858, 'wireshark.exe', 'gdkWindowChild'], [49810992, 'wireshark.exe', 'gdkWindowChild', [[33557978, 'wireshark.exe', 'gdkWindowChild'], [6163500, 'wireshark.exe', 'gdkWindowChild'], [5770630, 'wireshark.exe', 'gdkWindowChild'], [65736272, 'wireshark.exe', 'gdkWindowChild'], [97914624, 'wireshark.exe', 'gdkWindowChild'], [3542946, 'wireshark.exe', 'gdkWindowChild'], [20123550, 'wireshark.exe', 'gdkWindowChild'], [7999180, 'wireshark.exe', 'gdkWindowChild'], [48631252, 'wireshark.exe', 'gdkWindowChild'], [38735296, 'wireshark.exe', 'gdkWindowChild']]], [33557978, 'wireshark.exe', 'gdkWindowChild'], [6163500, 'wireshark.exe', 'gdkWindowChild'], [5770630, 'wireshark.exe', 'gdkWindowChild'], [65736272, 'wireshark.exe', 'gdkWindowChild'], [97914624, 'wireshark.exe', 'gdkWindowChild'], [3542946, 'wireshark.exe', 'gdkWindowChild'], [20123550, 'wireshark.exe', 'gdkWindowChild'], [7999180, 'wireshark.exe', 'gdkWindowChild'], [48631252, 'wireshark.exe', 'gdkWindowChild'], [38735296, 'wireshark.exe', 'gdkWindowChild'], [59248144, 'wireshark.exe', 'gdkWindowChild'], [10096344, 'wireshark.exe', 'gdkWindowChild']]
3 
4 Process finished with exit code 0
dumpWindow输出

1.3.6 调用一个窗体

初始化一个窗体之后,可以通过模糊匹配的方式操作该app中的一个窗体,变量名可以是该窗体的部分或者全部。

比如按照1.3.5 (一) 方法进行遍历

dilagHandle = win32gui.FindWindow("gdkWindowToplevel","Wireshark: Capture Interfaces")

traverse_func(dilagHandle)

输出的部分信息如下

Control Identifiers:

Dialog - 'Wireshark: Capture Interfaces'    (L60, T148, R679, B331)

['Wireshark: Capture Interfaces', 'Dialog', 'Wireshark: Capture InterfacesDialog']

child_window(title="Wireshark: Capture Interfaces", control_type="Window")

   |

   | Pane - 'wireshark.exe'    (L68, T178, R671, B323)

   | ['wireshark.exePane', 'wireshark.exe', 'Pane', 'wireshark.exePane0', 'wireshark.exePane1', 'wireshark.exe0', 'wireshark.exe1', 'Pane0', 'Pane1']

   | child_window(title="wireshark.exe", control_type="Pane")

   |    |

   |    | Pane - 'wireshark.exe'    (L70, T180, R669, B321)

   |    | ['wireshark.exePane2', 'wireshark.exe2', 'Pane2']

   |    | child_window(title="wireshark.exe", control_type="Pane")

   |    |    |

   |    |    | Pane - 'wireshark.exe'    (L70, T180, R669, B321)

   |    |    | ['wireshark.exePane3', 'wireshark.exe3', 'Pane3']

   |    |    | child_window(title="wireshark.exe", control_type="Pane")

   |    |    |    |

   |    |    |    | Pane - 'wireshark.exe'    (L508, T245, R547, B272)

   |    |    |    | ['wireshark.exePane10', 'wireshark.exe10', 'Pane10']

   |    |    |    | child_window(title="wireshark.exe", control_type="Pane")

可看到该top窗口下,有一个标题为” Wireshark: Capture Interfaces”的子窗口,可使用如下方法定位该子窗口。

dilagApp = Application(backend='uia').connect(handle=dilagHandle)

dilagApp["Wireshark: Capture Interfaces"] #方法一

dilagApp.WiresharkCaptureInterfaces #方法二

1.3.7 调用一个控件

Pywinauto使用以下顺序定位一个控件

1:控件的标题 title

2:控件的类名 friendly class

3:控件的标题加类名 title + friendly class

还是依照1.3.6中遍历出的信息继续写几个实例。

Top窗口的标题为"Wireshark: Capture Interfaces",top窗口下有一个标题为” Wireshark: Capture Interfaces”的子窗口, 子窗口下边有个控件标题为” wireshark.exe”,类型为” Pane”,且编号为10,则可以通过如下方式定位该控件:dilagApp.WiresharkCaptureInterfaces['wireshark.exePane10'

dilagApp.WiresharkCaptureInterfaces['wireshark.exe10'] dilagApp.WiresharkCaptureInterfaces['Pane10'] dilagApp.WiresharkCaptureInterfaces.wiresharkexePane10 dilagApp.WiresharkCaptureInterfaces.wiresharkexe10 dilagApp.WiresharkCaptureInterfaces.Pane10

1.3.8 控件信息查看

如下是获取常用属性的操作,其中friendly_class_name()、window_text()、class_name()、rectangle()用得比较多。

topHandle = win32gui.FindWindow("gdkWindowToplevel", "The Wireshark Network Analyzer")
topApp = Application(backend='uia').connect(handle=topHandle)
control = topApp["The Wireshark Network Analyzer"].wiresharkexe43
print(control.get_properties()) #Return the properties of the control as a dictionary.
print(control.friendly_class_name()) #Return the friendly class name for the control ==> Pane
print(control.window_text()) #Window text of the element ==> wireshark.exe
print(control.class_name()) #Return the class name of the elenemt ==> gdkWindowChild
print(control.is_visible()) #Whether the element is visible or not ==> True
print(control.is_enabled()) #Whether the element is enabled or not ==> True
print(control.rectangle()) #Return the rectangle of element ==> (L88, T145, R459, B166)
print(control.writable_props) #Return the list of the default properties to be written. ==>

1.3.9 控件基本操作

控件定位到之后,就可以对该控制执行鼠标、键盘等事件了。如下是几种常见操作:

#获取焦点

dilagApp.WiresharkCaptureInterfaces['Pane10'].set_focus()

#单击控件
dilagApp.WiresharkCaptureInterfaces['Pane10'].click_input()


#移动鼠标到控件
dilagApp.WiresharkCaptureInterfaces['Pane10'].click_input(button="move")


#双击控件
dilagApp.WiresharkCaptureInterfaces['Pane10'].double_click_input()


#右击控件
dilagApp.WiresharkCaptureInterfaces['Pane10'].right_click_input()

#控件获得焦点,如果该窗口被遮蔽了,会显示到顶层窗口,由不可见变成可见。
dilagApp.WiresharkCaptureInterfaces['Pane10'].set_focus()


#给控件发送组合键, “^I”就是发送ctrl+I的意思。
topApp = Application(backend='uia').connect(handle=topHandle)#relate to Application by handle
topApp.Dialog.type_keys("^I")

常用的按键

Ctrl

^

Alt

%

Shift

+

也可以指定鼠标点击的位置,使用例子如下:

left, top, right, bottom = win32gui.GetWindowRect(hwnd)

win32api.SetCursorPos((left + 40, top + 80))

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)

 

对于控件friendlyclassname值不一样的,控件的这个值可以通过control.friendly_class_name()获得还有各自特定的事件,常用的见下表如:

 

 

 

使用示例:

dilagApp.top_window().child_window(title="文件名(N):", control_type="Edit").set_edit_text(u'C:\\Users\\20002106\Desktop\\temp\\7.pcap')

1.3.10 结束应用程序

os.system("taskkill /F /IM  wireshark.exe")

1.4 Reference

[1].          http://pywinauto.github.io

A.3 win32_control

A.3.1 Button

 1 class ButtonWrapper(hwndwrapper.HwndWrapper)
 2 
 3     friendlyclassname = "Button"
 4 
 5     def get_check_state(self):
 6         """
 7         Return the check state of the checkbox
 8 
 9         The check state is represented by an integer
10         0 - unchecked
11         1 - checked
12         2 - indeterminate
13 
14         The following constants are defined in the win32defines module
15         BST_UNCHECKED = 0
16         BST_CHECKED = 1
17         BST_INDETERMINATE = 2
18         """
19 
20     def check(self):
21         """Check a checkbox"""
22 
23     def uncheck(self):
24         """Uncheck a checkbox"""
25 
26     def check_by_click(self):
27         """Check the CheckBox control by click() method"""
28 
29     def uncheck_by_click(self):
30         """Uncheck the CheckBox control by click() method"""
31 
32     def check_by_click_input(self):
33         """Check the CheckBox control by click_input() method"""
34 
35     def uncheck_by_click_input(self):
36         """Uncheck the CheckBox control by click_input() method"""
ButtonWrapper

A.3.2 ComboBox

 1 class ComboBoxWrapper(hwndwrapper.HwndWrapper):
 2 
 3     friendlyclassname = "ComboBox"
 4 
 5     def item_count(self):
 6         """Return the number of items in the combobox"""
 7 
 8     def selected_index(self):
 9         """Return the selected index"""
10 
11     def selected_text(self):
12         """Return the selected text"""
13 
14     def item_data(self, item):
15         """Returns the item data associated with the item if any"""
16 
17     def item_texts(self):
18         """Return the text of the items of the combobox"""
19 
20     def texts(self):
21         """Return the text of the items in the combobox"""
22 
23     def get_properties(self):
24         """Return the properties of the control as a dictionary"""
25 
26     def select(self, item):
27         """Select the ComboBox item
28 
29         item can be either a 0 based index of the item to select
30         or it can be the string that you want to select
31         """
ComboBoxWrapper

A.3.3 ListBox

 1 class ListBoxWrapper(hwndwrapper.HwndWrapper)
 2 
 3     friendlyclassname = "ListBox"
 4 
 5     def is_single_selection(self):
 6         """Check whether the listbox has single selection mode."""
 7 
 8     def selected_indices(self):
 9         """The currently selected indices of the listbox"""
10 
11     def item_count(self):
12         """Return the number of items in the ListBox"""
13 
14     def item_data(self, i):
15         """Return the item_data if any associted with the item"""
16 
17     def item_texts(self):
18         """Return the text of the items of the listbox"""
19 
20     def item_rect(self, item):
21         """Return the rect of the item"""
22 
23     def texts(self):
24         """Return the texts of the control"""
25 
26     def select(self, item, select=True):
27         """Select the ListBox item
28 
29         item can be either a 0 based index of the item to select
30         or it can be the string that you want to select
31         """
32 
33     def set_item_focus(self, item):
34         """Set the ListBox focus to the item at index"""
35 
36     def get_item_focus(self):
37         """Retrun the index of current selection in a ListBox"""
ListBoxWrapper

A.3.4 Edit

 1 class EditWrapper(hwndwrapper.HwndWrapper):
 2 
 3     friendlyclassname = "Edit"
 4 
 5     def line_count(self):
 6         """Return how many lines there are in the Edit"""
 7 
 8     def line_length(self, line_index):
 9         """Return how many characters there are in the line"""
10 
11     def get_line(self, line_index):
12         """Return the line specified"""
13 
14     def texts(self):
15         """Get the text of the edit control"""
16 
17     def text_block(self):
18         """Get the text of the edit control"""
19 
20     def selection_indices(self):
21         """The start and end indices of the current selection"""
22 
23     def set_window_text(self, text, append = False):
24         """Override set_window_text for edit controls because it should not be
25         used for Edit controls.
26 
27         Edit Controls should either use set_edit_text() or type_keys() to modify
28         the contents of the edit control."""
29 
30     def set_edit_text(self, text, pos_start = None, pos_end = None):
31         """Set the text of the edit control"""
32 
33     def select(self, start = 0, end = None):
34         """Set the edit selection of the edit control"""
EditWrapper

猜你喜欢

转载自www.cnblogs.com/aimmiao/p/12745892.html