TypeKeys()
またはSendKeysCtypes()
(現在のバージョンでpywinauto
は、このモジュールを使用してキー ストロークを送信します) モジュールは使用できません。これはSendInput
、アクティブなアプリケーションでのみ機能する win API を使用しているためです (アプリケーションが最小化されている場合、アプリケーションはアクティブになりません。
とはいえ、次のことが役に立つかもしれません: 編集テキストを変更します:
app.Dialog.Edit.SetEditText("Your Text")
ボタンまたはチェックボックスなどをクリックします。
app.Dialog.ButtonEtc.Click()
これらは、バックグラウンドにあるアプリケーションで機能します。ウィンドウを非表示にしようとしましたが、上記は機能しませんでした:((デフォルトでは、ほとんどのpywinautoはウィンドウが表示されていることを確認しようとしているためです+ Enabled.
非表示のウィンドウを取得するには、次を使用できます。
hidden_win = app.window_(title = "Untitled - Notepad", visible_only = False)
子ウィンドウを取得するには、次のようにします。
edit_control = hidden_win.ChildWindow(visible_only = False, class_name = "Edit")
しかし、残念なことに、pywinauto を使用するときに少し行き詰まりますedit_control.SetEditText(...)
(ウィンドウが表示されていることを確認するため:()
これは、編集テキストの値を設定するために多かれ少なかれ機能するハッキングされたコードです ( SetEditText と Select は両方とも pywinauto からコピーされ、次のようにわずかに変更されていることに注意してください。
コントロールが表示されていることを確認しません
それらはスタンドアロン関数です
コード:
import win32gui
import win32con
import sys
import os
sys.path.append(os.path.abspath('.'))
print sys.path[-1]
from pywinauto import application
from pywinauto import win32defines
from pywinauto import win32functions
from pywinauto.timings import Timings
import time
import ctypes
#-----------------------------------------------------------
def Select(win, start = 0, end = None):
"Set the edit selection of the edit control"
# if we have been asked to select a string
if isinstance(start, basestring):
string_to_select = start
#
start = win.TextBlock().index(string_to_select)
if end is None:
end = start + len(string_to_select)
if end is None:
end = -1
win.SendMessageTimeout(win32defines.EM_SETSEL, start, end)
# give the control a chance to catch up before continuing
win32functions.WaitGuiThreadIdle(win)
time.sleep(Timings.after_editselect_wait)
# return this control so that actions can be chained.
return win
#-----------------------------------------------------------
def SetEditText(win, text, pos_start = None, pos_end = None):
"Set the text of the edit control"
# allow one or both of pos_start and pos_end to be None
if pos_start is not None or pos_end is not None:
# if only one has been specified - then set the other
# to the current selection start or end
start, end = win.SelectionIndices()
if pos_start is None:
pos_start = start
if pos_end is None:
pos_end = end
# set the selection if either start or end has
# been specified
win.Select(pos_start, pos_end)
else:
Select(win)
# replace the selection with
text = ctypes.c_wchar_p(unicode(text))
win.SendMessageTimeout(win32defines.EM_REPLACESEL, True, text)
win32functions.WaitGuiThreadIdle(win)
time.sleep(Timings.after_editsetedittext_wait)
# return this control so that actions can be chained.
return win
# this may be useful if you just want to send a keydown/keyup combination
def keystroke(hwnd, key):
win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, key, 0)
win32gui.PostMessage(hwnd, win32con.WM_KEYUP, key, 3 < 30)
app = application.Application.start('notepad')
# hide the window
win32gui.ShowWindow(app.window_(title = "Untitled - Notepad", visible_only = False).handle, win32defines.SW_HIDE)
print "See it is hidden :)"
time.sleep(2)
# get tehe edit control and set it's text
edit = app.window_(title = "Untitled - Notepad", visible_only = False).ChildWindow(visible_only = False, class_name = "Edit")
SetEditText(edit, "The edit text has been set")
# show the window again
win32gui.ShowWindow(app.window_(title = "Untitled - Notepad", visible_only = False).handle, win32defines.SW_SHOW)
これは、pywinauto がどのように改善されるかを示す良い例です (このようなことをより簡単にする低レベルのライブラリを提供することによって)