1

[1] The code is:

import pywinauto
app = pywinauto.Application()
mainApplication = app.window_(title_re = ".*Environment.*")
mainApplication.Minimize()
mainApplication.Edit.SetText("test", with_spaces=True)
mainApplication.Edit.SetText("{ENTER}")

[2] The output is:

  File "C:\Python27\lib\site-packages\pywinauto\application.py", line 239, in __getattr__
    ctrls = _resolve_control(self.criteria)
  File "C:\Python27\lib\site-packages\pywinauto\application.py", line 754, in _resolve_control
    raise e.original_exception
WindowNotFoundError

Note: If I use 'TypeKeys' method I do not encounter any problem, but I want to write in an app. even if focus is not set on it.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
george
  • 685
  • 2
  • 9
  • 22

1 Answers1

1

Unfortunately cmd.exe has no any controls inside the window. So you cannot send WM_SETTEXT message to it. That's why SetText isn't working. And it will never work for cmd.exe because GUI automation tool is for GUI, not for command line.

Generally you can interact with cmd.exe process through pipes using standard module subprocess (class Popen).

You can try the following:

  1. run cmd.exe through Popen,
  2. Connect(path='cmd.exe') using pywinauto,
  3. call app.Window_().Minimize(),
  4. send keys using p.stdin.write(someInput) where p is a Popen object.

When you're using pipes, there are some pitfalls with potential dead lock of input and output streams. Here is one method to workaround some issues: Running an interactive command from within python

Community
  • 1
  • 1
Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • 1
    Thanks a lot for helping me out. I found a way to writetext to inactive cmmand prompt. import win32gui import win32con import win32api handle = win32gui.FindWindow(None, u'Administrator: Environment') print handle for text in "test": win32api.SendMessage(handle, win32con.WM_CHAR, ord(text), 0) – george Oct 19 '15 at 13:36
  • Great finding! We will implement `SendChars` method in pywinauto using `WM_CHAR`. Is there some way to retrieve console text using some message? If you could find it, please let us know. – Vasily Ryabov Oct 19 '15 at 14:17