imhotep
imhotep

Reputation: 67

Select window by title with pywinauto

Using pywinauto I am trying to close a popup in the software when it arises as it is conditional.

def get_window():
    app = pywinauto.application.Application(backend="uia")
    app.connect(path='gofer.exe')
    #app.Properties.print_control_identifiers()
    trade = app.window(best_match='Warning:')
    # trade.wrapper_object().close
    print(trade)
    if trade == 'Warning:':
        print("You see the Window")
        # press enter key to displace
        # start next action
    else:
        print("Naw, No window bro")
        # Log to file 
        pass

The output of the print(trade)is:

<pywinauto.application.WindowSpecification object at 0x0000019B8296DBA8>

So I know it is at least working, but not going where I want. The warning is a window that pops up and has a title of "Warning" according to spy++.

However, I have not been able to print the window data... Although the window is a popup, it is not a toast popup if that makes a difference. It is a dlg window.

The properties prints a dict that refers only to the main program and hints at dialog windows, but never specifies the attributes. Even when searching the main program title, I am unable to make this work correctly.

Upvotes: 1

Views: 7180

Answers (1)

AllenMoh
AllenMoh

Reputation: 476

This is what I am doing to identify popup windows. Essentially you want to create a Dialog (dlg) representing the window that is the parent of the popup:

app = pywinauto.application.Application(backend="uia")
app.connect(path='gofer.exe')

# Using regular expression to create a dialog of the gofer.exe app
# I am assuming the title will match "*Gofer*" eg: "Gofer the Application"
dlg = app.window(title_re=".*Gofer.*")

# Now I am going to identify the title of the popup window:
dlg.print_ctrl_ids()

# If you did the last step correctly, the output will look something like:
#Control Identifiers:
#Dialog - 'Gofer The Application'    (L688, T518, R1065, B1006)
#[u'Dialog', u'Gofer Dialog']
#child_window(title="Warning: ", control_type="Window")
   #|
   #| Image - ''    (L717, T589, R749, B622)
   #| [u'', u'0', u'Image1', u'Image0', 'Image', u'1']
   #| child_window(auto_id="13057", control_type="Image")
   #|
   #| Image - ''    (L717, T630, R1035, B632)
   #| ['Image2', u'2']
   #| child_window(auto_id="13095", control_type="Image")
   #|


# Now using the same title and control type for the popup that we identified
# We check to see if it exists as follows:

if dlg.child_window(title="Warning:", control_type="Window").exists():
    print("Bro, you got a pop-up bro...")

else:
    print("No popup Bro...")

Upvotes: 1

Related Questions