Skip to main content
 首页 » 编程设计

python之需要一种使用 pywinauto 在具有控件 ID 的窗口中访问控件的方法

2024年10月17日9lyhabc

通过使用类名,可以使用 pywinauto 函数从窗口中检索控件的值。最近这在尝试用于 SAP 时似乎是个问题,因为类名是动态的。有没有办法获取控件的动态类名,或者有没有办法使用控件 ID 而不是类名来访问控件。

from pywinauto.application import Application 
 
app = Application().Connect(title=u'Create Purchase Order', class_name='SAP_FRONTEND_SESSION') 
sapfrontendsession = app.SAP_FRONTEND_SESSION 
print sapfrontendsession.PrintControlIdentifiers() 
 
it gives 
 
Control Identifiers: 
Afx:62F20000:0:00010003:00000010:00000000 - ''   (L14, T38, R1352, B69) 
	'' '0' '1' 'Afx:62F20000:0:00010003:00000010:00000000' () 
ComboBox - ''   (L54, T43, R196, B65) 
	'2' 'ComboBox' () 
Edit - ''   (L57, T46, R174, B62) 
	'3' 'Edit' () 
AfxWnd110 - ''   (L14, T69, R1352, B78) 
	'6' 'AfxWnd1103' () 
AfxWnd110 - ''   (L65, T78, R1352, B109) 
	'7' 'AfxWnd1104' () 
Afx:62F20000:8:00010003:00000000:00000000 - ''   (L14, T109, R1352, B143) 
	'8' 'Afx:62F20000:8:00010003:00000000:00000000' () 
Button - ''   (L24, T115, R170, B135) 
	'9' 'Button' 'Button0' 'Button1' () 
Button - ''   (L174, T115, R180, B135) 
	'10' 'Button2' () 
Button - ''   (L184, T115, R204, B135) 
	'11' 'Button3' () 
Button - ''   (L208, T115, R228, B135) 
	'12' 'Button4' () 
Button - ''   (L232, T115, R270, B135) 
	'13' 'Button5' () 
Button - ''   (L274, T115, R294, B135) 
	'14' 'Button6' () 
Button - ''   (L298, T115, R304, B135) 
	'15' 'Button7' () 
Button - ''   (L308, T115, R414, B135) 
	'16' 'Button8' () 
Button - ''   (L418, T115, R483, B135) 
	'17' 'Button9' () 
Button - ''   (L487, T115, R507, B135) 
	'18' 'Button10' () 
Button - ''   (L511, T115, R634, B135) 
	'19' 'Button11' () 
Button - ''   (L638, T115, R644, B135) 
	'20' 'Button12' () 
Button - ''   (L648, T115, R761, B135) 
	'21' 'Button13' () 
Button - ''   (L765, T115, R890, B135) 
	'22' 'Button14' () 
Afx:62F20000:8:00010003:00000010:00000000 - 'Standard PO created under the number 4500018380'   (L14, T699, R1352, B728) 
	'Afx:62F20000:8:00010003:00000010:00000000' 'Standard PO created under the number 4500018380' 'Standard PO created under the number 4500018380Afx:62F20000:8:00010003:00000010:00000000' () 
Afx:62F20000:1008 - ''   (L14, T143, R1352, B699) 
	'27' 'Afx:62F20000:1008' () 
Custom Container Class - 'Custom  Container'   (L17, T151, R44, B172) 
	'Custom  Container4' 'Custom  ContainerCustom Container Class4' 'Custom Container Class4' () 
Shell Window Class - 'Control  Container'   (L17, T151, R44, B172) 
	'Control  Container4' 'Control  ContainerShell Window Class4' 'Shell Window Class4' () 
SAPImage - ''   (L17, T151, R44, B172) 
	'34' 'SAPImage2' () 
GOSContainer Class - 'Gos Container'   (L14, T78, R65, B109) 
	'GOSContainer Class' 'Gos Container' 'Gos ContainerGOSContainer Class' () 
Shell Window Class - 'Control  Container'   (L24, T82, R65, B104) 
	'Control  Container6' 'Control  ContainerShell Window Class6' 'Shell Window Class6' () 
ATL:664AB3D8 - ''   (L24, T82, R66, B104) 
	'40' 'ATL:664AB3D8' () 
SysPager - 'SAPPagerForToolbar'   (L24, T82, R95, B104) 
	'Pager' 'SAPPagerForToolbar' 'SAPPagerForToolbarPager' () 
ToolbarWindow32 - ''   (L24, T82, R95, B104) 
	'41' 'Toolbar' ()

控件需要是 Afx:62F20000:8:00010003:00000010:00000000 但它在每次运行时都会发生变化。当使用 swapy 检查时,controlID 是相同的。是否有可能使用 controlID 可以检索此控件上的文本。

请您参考如下方法:

控件 ID 通常不保证是永久的。我相信通过正则表达式匹配 class_name 会更可靠。

app.Dialog.child_window(class_name_re='^constant_part.*$').some_method() 
 
# though Control ID can be used so 
app.Dialog.child_window(control_id=0x5D).some_method() 
# or 
app.Dialog.child_window(class_name_re='^constant_part.*$', found_index=0).some_method() 

同样适用于 title_re。在您的特定情况下:

sapfrontednsession.child_window(title_re='^Standard PO created under the number \d+$').click() 

可以使用 MS Visual Studio 中包含的 Spy++ 实用程序找到控件 ID(可从“开始”菜单获得)。

sapfrontednsession.child_window(control_id=<found ID in hex>).click()