Search code examples
pythonpywinauto

pywinauto -how to handle alert window control


I have done my form in VB. I cannot access the child window controls. For instance, the alert box appears after submit button is clicked. Here is my code:

# used backend="uia"

import sys
import pyautogui
from pywinauto.application import Application
import time
print("test")
app=Application().start()
app.Form1.Edit4.type_keys("go")
app.Form1.Edit3.type_keys("12")
app.Form1.Male.click()
app.Form1.ComboBox.type_keys("in")
app.Form1.Edit2.type_keys("33")
app.Form1.Submit.click()
app.Form1.Submit.print_control_identifiers()
app.Success.print_control_identifiers()
app.Form1.Success.click()

Success is the name of child window.


Solution

  • you write that you've used backend="uia" but the code Application().start() uses default backend which is "win32". You have to use Application(backend="uia").start() to choose "uia".

    Note: for "win32" backend the alert window is a top-level window. So you need app.Success.OK.click() to click OK button on it. For backend="uia" alert window will be child of "Form1".

    EDIT: this code should work:

    app.Form1.Success.OKButton.click() # alias of .invoke();
    # see IsInvokePatternSupported == True in Inspect.exe
    
    # or
    app.Form1.Success.OKButton.click_input() # real click
    

    EDIT2: It may be timing issue. pywinauto has default timeout 5 seconds waiting for dialog appearance. If dialog appears after more than 5 seconds, you need something like that: app.Form1.Success.wait('visible', timeout=20).

    Another possible issue is blocking behavior of app.Form1.Submit.click() which calls InvokePattern. Sometimes this pattern implementation waits for dialog closing (this is app side issue though). You might have to change this to app.Form1.Submit.click_input().