3

I want to automate my test case for my desktop application(windows application) where I need to get/fetch the currently focused element. Can someone please help me or guide in this regard?

I tried with GetFocus method of pywinauto which just returned the details of Active Window but I need focussed element info

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Do you mean the child element with keyboard focus? – Vasily Ryabov Jun 27 '19 at 11:55
  • I want to get the current focussed element, for e.g the user open a personal information form and the foccused element is the textbox of Name field. Now I want to get to the currently focused item which is Name field in this case. I have used get_focus but that did not work for me because getfocus() gives me the window, I need particular item/element. – Noman Masood Jun 28 '19 at 16:07

1 Answers1

2

Currently it's not implemented, but the workaround is possible. For "win32" backend:

import win32gui
from pywinauto.controls.hwndwrapper import HwndWrapper
keyboard_focused = HwndWrapper(win32gui.GetFocus())

For "uia" backend (may not work for WPF app):

import win32gui
from pywinauto.controls.uiawrapper import UIAWrapper
keyboard_focused = UIAWrapper(win32gui.GetFocus())

I've filed issue #760 to implement it later.

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • 2
    win32gui.GetForegroundWindow() gave me better success than GetFocus(), which always returned 0. Also, to install win32gui you should install the pywin32 package (trying to pip install win32gui gives errors). – voxoid May 28 '21 at 17:17