Mat
Mat

Reputation: 91

Getting text from window (notepad)

I've started learning python few days ago and stuck while I was trying to get text from notepad

the code below returning me Window title but when I'm using win32gui.GetWindowText(control) it returns null. Could someone mark my mistake here? Thank you in advance

import time
import win32gui

while True:
    window = win32gui.GetForegroundWindow()
    title = win32gui.GetWindowText(window)
    control = win32gui.FindWindowEx(window, 0, 'Edit', None)
    print('text: ', win32gui.GetWindowText(window))
    print('control to %s, window to %s' %(str(control),  str(window)))
    time.sleep(1)

Upvotes: 2

Views: 755

Answers (1)

Anders
Anders

Reputation: 101636

MSDN tells you twice in the documentation that this will not work!

GetWindowText cannot retrieve the text of a control in another application

To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.

Upvotes: 3

Related Questions