ui-automationpywinauto

pywinauto finds a button it doesn't get clicked


import time, sys
from pywinauto.application import Application
import pygetwindow as gw

def reload_app():
    # Redirect sys.stdout to a file
    path = r"C:\Program Files (x86)\App\app.exe"
    original_stdout = sys.stdout
    with open("app_info.txt", 'w', encoding='utf-8') as f:
        sys.stdout = f

        vpn_window = gw.getWindowsWithTitle("App")
        if app_window:
            app_window[0].activate()

        app = Application(backend="uia").connect(path=path)
        window = app.window(title_re=r"App")

        if window.exists():
            window.print_control_identifiers()
            disconnect_button = window.child_window(title="Disconnect", control_type="Button")
    
            if disconnect_button.exists():
                disconnect_button.click()
                print("Clicked the Disconnect button.")
            else:
                print("Disconnect button not found.")
        else:
            print("Nothing...!")
        
    # Restore the original sys.stdout
    sys.stdout = original_stdout

reload_vpn()

The button itself gets found wiht no problem but it never gets clicked and doesn't raise an error. Here is output of main input print_control_identifiers()

Control Identifiers:

Dialog - 'App'    (L448, T223, R1472, B857)
['Dialog', 'App', 'AppDialog']
child_window(title="PlanetVPN", control_type="Window")

    ...
    Button - 'Disconnect'    (L1066, T595, R1226, B635)
   | ['Button6', 'Disconnect', 'DisconnectButton']
   | child_window(title="Disconnect", control_type="Button")

The application itself requires administrator privilegies. I could not even find a process by path before I run my IDE with administrator privilegies. After that at least I managed to get an info about control identifers. But now I am struggling to click some buttons that I found.


Solution

  • Solved this by using .click_input() instead .click() method