[英]How can I use 'SetText' in command prompt?
[1] The code is: [1]代码为:
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: [2]输出为:
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. 注意:如果使用'TypeKeys'方法,我不会遇到任何问题,但是我想在应用程序中编写。 even if focus is not set on it. 即使未将重点放在上面。
Unfortunately cmd.exe has no any controls inside the window. 不幸的是,cmd.exe在窗口内没有任何控件。 So you cannot send WM_SETTEXT
message to it. 因此,您无法向其发送WM_SETTEXT
消息。 That's why SetText
isn't working. 这就是SetText
无法正常工作的原因。 And it will never work for cmd.exe because GUI automation tool is for GUI, not for command line. 它永远不会对cmd.exe起作用,因为GUI自动化工具是针对GUI而不是命令行。
Generally you can interact with cmd.exe process through pipes using standard module subprocess
(class Popen
). 通常,您可以使用标准模块subprocess
进程(类Popen
)通过管道与cmd.exe进程进行交互。
You can try the following: 您可以尝试以下方法:
cmd.exe
through Popen
, 通过Popen
运行cmd.exe
, Connect(path='cmd.exe')
using pywinauto, 使用pywinauto Connect(path='cmd.exe')
, app.Window_().Minimize()
, 调用app.Window_().Minimize()
, p.stdin.write(someInput)
where p
is a Popen
object. 发送用密钥p.stdin.write(someInput)
其中p
是一个Popen
对象。 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 这是解决某些问题的一种方法: 从python内部运行交互式命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.