pywinauto 找到一个没有被点击的按钮

问题描述 投票:0回答:1
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()

按钮本身被发现没有问题,但它永远不会被点击,也不会引发错误。这是主输入的输出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")

应用程序本身需要管理员权限。在使用管理员权限运行 IDE 之前,我什至无法按路径找到进程。之后,至少我设法获得了有关控制标识符的信息。但现在我很难点击我找到的一些按钮。

ui-automation pywinauto
1个回答
0
投票

通过使用 .click_input() 而不是 .click() 方法解决了这个问题

© www.soinside.com 2019 - 2024. All rights reserved.