【发布时间】:2021-01-01 18:13:48
【问题描述】:
我想检索用户正在输入的文本框的值
到目前为止,我已经得到了这段代码,它可以输出我拥有键盘焦点的元素中所写的内容。 但是,它只适用于记事本的文本区域,当我尝试在其他应用程序上使用它时,它会打印诸如“Chrome Legacy Window”之类的内容
from ctypes import windll, create_unicode_buffer
import win32gui
import win32con
import time
def getForegroundWindowTitle():
hWnd = windll.user32.GetForegroundWindow()
length = windll.user32.GetWindowTextLengthW(hWnd)
buf = create_unicode_buffer(length + 1)
windll.user32.GetWindowTextW(hWnd, buf, length + 1)
if buf.value:
return buf.value
else:
return None
time.sleep(3)
windowHandle = win32gui.FindWindow(None, getForegroundWindowTitle())
childwindowHandle = win32gui.FindWindowEx(windowHandle, None, win32gui.GetFocus(), None)
buff = create_unicode_buffer(4096)
win32gui.SendMessage(childwindowHandle, win32con.WM_GETTEXT, 4096, buff) #0x000D
print(buff.value)
【问题讨论】:
-
为此使用 UI 自动化:docs.microsoft.com/en-us/windows/win32/winauto/… 与 python,我想你可以使用这个:
-
我设法用 pywinauto ``` from pywinauto.application import Application import time app = Application(backend="uia").start('notepad.exe') time.sleep 获得了一个文本框的值(3) print(app["*Sans titre\xa0- Bloc-notes"]["EDIT"].get_value()) ```但是我还是需要指定元素("Edit")
-
使用 Windows SDK 中的检查工具检查 UI 自动化树和每个元素支持的 UIA 模式
标签: python winapi ctypes pywin32