[Python] pywinauto (Win32) won’t find Edit until I manually click it—how to force a hidden...

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I’m automating a legacy Win32 app with pywinauto’s Win32 backend. When I dump the control tree, I see that an Edit control named Edit62 exists in the panel, but any attempt to access it fails with ElementNotFoundError—until I manually click or TAB into its parent. Only after that interaction does panel.Edit62 become visible to pywinauto.

    Here’s a minimal repro of my code:

    from pywinauto import Application

    # Connect to the running app
    app = Application(backend="win32").connect(title_re="Domínio Folha.*")
    main_win = app.window(title_re="Domínio Folha.*")

    # Locate the “Cadastro de Empregados” panel
    panel = main_win.child_window(
    title="Cadastro de Empregados",
    class_name="FNWND3190"
    )

    # Print identifiers at depth=3
    panel.print_control_identifiers(depth=3)


    Output snippet (only buttons appear at depth 3):

    | pbdw190 - '' (L491, T266, R1449, B344)
    | ['|<pbdw19035', 'pbdw19036']
    | child_window(class_name="pbdw190")
    | |
    | | Edit - '' (L491, T266, R491, B266)
    | | ['Edit62']
    | | child_window(class_name="Edit")
    | |
    | | Edit - '' (L491, T266, R491, B266)
    | | ['Edit63']
    | | child_window(class_name="Edit")
    | |
    | | PBEDIT190 - '928' (L401, T249, R491, B267)
    | | ['928', '928PBEDIT190', 'PBEDIT190139']
    | | child_window(title="928", class_name="PBEDIT190")



    Then I try:

    # This raises ElementNotFoundError
    panel.Edit62.wait('exists ready', timeout=5)


    But if I manually click or press TAB so that the real UI gets focus on that region, then panel.Edit62 resolves successfully.

    What I’ve tried:

    panel.set_focus() and even Win32’s SetForegroundWindow

    increasing timeouts

    wrapper.invalidate_cache()

    None of these make Edit62 appear until I physically click the panel.

    I could resort to sending repeated TAB keys and filling fields as focus moves, but I’d prefer a more accurate approach: programmatically focusing the specific input control, instead of spamming TAB to reach it.

    Question: How can I programmatically force the Edit62 control (and other dynamically loaded children) to become visible to pywinauto without a manual click or TAB? Is there a recommended API call or sequence of messages to send so the app loads that control in advance?

    Thanks!

    Continue reading...

Compartilhe esta Página