print control identifiers to a file

Viewed 524

I am trying to print control identifiers to a file in python for a dekstop application using pywinauto. But since it has some ascii characters, its giving the error below

handle.print_control_identifiers(filename="page_contents.txt")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\test\PycharmProjects\LearnPythontest\venv\lib\site-packages\pywinauto\application.py", line 696, in print_control_identifiers
    print_identifiers([this_ctrl, ], log_func=log_func)
  File "C:\Users\test\PycharmProjects\LearnPythontest\venv\lib\site-packages\pywinauto\application.py", line 685, in print_identifiers
    print_identifiers(ctrl.children(), current_depth + 1, log_func)
  File "C:\Users\test\PycharmProjects\LearnPythontest\venv\lib\site-packages\pywinauto\application.py", line 685, in print_identifiers
    print_identifiers(ctrl.children(), current_depth + 1, log_func)
  File "C:\Users\test\PycharmProjects\LearnPythontest\venv\lib\site-packages\pywinauto\application.py", line 685, in print_identifiers
    print_identifiers(ctrl.children(), current_depth + 1, log_func)
  [Previous line repeated 1 more time]
  File "C:\Users\test\PycharmProjects\LearnPythontest\venv\lib\site-packages\pywinauto\application.py", line 681, in print_identifiers
    log_func(output)
  File "C:\Users\test\PycharmProjects\LearnPythontest\venv\lib\site-packages\pywinauto\application.py", line 694, in log_func
    log_file.write(str(msg) + os.linesep)
  File "C:\Users\test\AppData\Local\Programs\Python\Python37\lib\codecs.py", line 721, in write
    return self.writer.write(data)
  File "C:\Users\test\AppData\Local\Programs\Python\Python37\lib\codecs.py", line 377, in write
    data, consumed = self.encode(object, self.errors)
  File "C:\Users\test\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character '\u25bc' in position 70: character maps to undefined

I have few lines in the output like this,

|    |    |    | Static - 'Speed:  6.50 Mbps  ▼'    (L884, T711, R1109, B731)
|    |    |    | ['Speed:  6.50 Mbps  ▼', 'Static22', 'Speed:  6.50 Mbps  ▼Static']
|    |    |    | child_window(title="Speed:  6.50 Mbps  ▼", control_type="Text")

I could see lines till the line above these lines are written to that file, but not anything after this Any help to resolve this and save the complete output to a file.

The handle.print_control_identifiers() is returning a <class 'NoneType'>. hence we can't iterate over it to match a few things in the output. So trying to save it to a file or so, and then do regex/match in that file

1 Answers

I did it this way:

from contextlib import redirect_stdout

with open("notepad_identifiers/_2.txt", "w", encoding="utf8") as f:
    with redirect_stdout(f):
        app.Dialog.print_control_identifiers()

We redirect standard output, so the result of print_control_identifiers() will go straight to file.

Related