Search code examples
pythonpywinauto

PyWinAuto automation of notepad example vs 'reallife' example


What I need to do is click a menu item and load a file. Love the well documented notepad example, which goes something like this...

from pywinauto import Application

app = Application (backend="uia").start("notepad.exe")
app.UntitledNotepad.menu_select("File->SaveAs")
Sub=app.UntitledNotepad.child_window(title_re="Save As", class_name="#32770")
Sub.FileNameCombo.type_keys("temp_12345.txt")    

Works Great. however, when I adapt this code for my application and try running menu_select an 'AttributeError' exception is raised. I am fairly certain this is because the menu strip in my application is of type uia_controls.MenuWrapper and does not support menu_select

I have tried a different approach - as shown below

app = Application(backend='uia').start(r"C:\Program Files (x86)\myapplication.exe")
time.sleep(1)
win = app.MyApplication
win['File'].select() # exapnd submenu
#Added AFTER I asked the question - i finally worked it out...
sub = win['File']
loadConfigMenuItem = (sub.children()[0])
loadConfigMenuItem.click_input() #print statement is executed
#loadConfigMenuItem.select() #print statement is NOT executed until I close the dialog box
print("If this prints, then I am a happy Man")

this expands the file menu. However, from this point on I am unable to access the child elements of this menu. Any Ideas please???


Solution

  • I answered my own question. Look at the code listing, especially

    loadConfigMenuItem.click_input()
    

    Key here is that this clicks the menu item and continues executing, thus enabling me to get a reference to the dialog box that appears after clicking the menu item

    Conversely, If you were to use

    loadConfigMenuItem.select()
    

    then no code is executed after this call until the dialog box is closed