Search code examples
pythonpywinauto

Why can't I write control identifiers to a file or assign them to a string or list


I'm using pywinauto to list the control identifiers of a particular application. I can do that just fine. However, I want to save those control identifiers to file, or better yet assign them to a string or list, but cannot write them or assign them .... Does anyone know a way to get these identifiers to a file or memory programmatically?

sample code:

import os
import time
from pywinauto import application
from SendKeys import SendKeys


app=application.Application()
app.start_(r"C:\Program Files\myapp.exe")


app.dlg.print_control_identifiers()

Control Identifiers:
Button - 'Exit'   (L900, T649, R975, B672)
        'Button' 'Button0' 'Button1' 'Exit' 'ExitButton'
Button - 'About'   (L339, T646, R410, B672)
        'About' 'AboutButton' 'Button2'
...
...
...

I tried the following:

my_App_ci = app.dlg.print_control_identifiers()

And:

my_App_ci = []
my_App_ci.append(app.dlg.print_control_identifiers())

to no avail ....


Solution

  • print_control_identifiers prints to stdout instead of returning a string. I did a quick look at the source and I could not see any functions to get them as strings, which is pretty crappy design IMHO.

    You could capture the information by reassigning sys.stdout to an StringIO object and get the string from that. Or read the source to see what print_control_identifiers does and make a version that returns a list of strings.