Using pywinauto, how do I get Text of static text object, given the handle of the parent dialog

D. L. Palmer picture D. L. Palmer · Sep 29, 2016 · Viewed 7.6k times · Source

1. My code:

print ('##########')  
app = pywinauto.application.Application()  
window_handle = pywinauto.findwindows.find_windows(title = u'My Dialog Name')  
my_handle = window_handle[0]  
window = app.window_(handle = my_handle)  
for x in window.Children():  
    print ('Child %s' % x)  
print ('##########')  

Output:
##########
Child: < pywinauto.controls.HwndWrapper.HwndWrapper object at 0x02C12890>
Child: < pywinauto.controls.win32_controls.ButtonWrapper object at 0x02C12870>
Child: < pywinauto.controls.HwndWrapper.HwndWrapper object at 0x02C128B0>
Child: < pywinauto.controls.win32_controls.StaticWrapper object at 0x02C128F0>
##########

I want the text of Child: pywinauto.controls.win32_controls.StaticWrapper object at 0x02C128F0>. To do so, I need the handle of that static text. I assume that I should be able to derive the handle to the static text from the parent dialog, but I have no idea how. I know that once I have the handle, I can just use window.Texts().

Apologies in advance for the formatting of my question, first timing it here. Thank you for any help.

Answer

vitswd picture vitswd · Sep 29, 2016

Why don't you try to locate your "Static" with pywinauto builtin name resolution? Let's say I have running Calculator application with text "78" on its result. I connect to the application and start looking for a "Static" element on "Calculator" dialog. Since I don't know the exact name I just try to guess it. I draw my debug outlines of different colors until I find the control I need. After I found the control I can easily read its text. (Read more in pywinauto docs here about specifying controls on a dialog.)

In [1]: from pywinauto.application import Application

In [2]: app = Application()

In [3]: app.connect(path="calc.exe")
Out[3]: <pywinauto.application.Application at 0x54362b0>

In [4]: app.Calculator.Static.DrawOutline()
Out[4]: 

In [5]: app.Calculator.Static2.DrawOutline("red")
Out[5]

In [6]: app.Calculator.Static3.DrawOutline("blue")
Out[6]

In [7]: app.Calculator.Static3.Texts()
Out[7]: [u'78']