首页 > 解决方案 > Pywinauto 无法找到/关闭弹出窗口

问题描述

源代码

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    app = Application(backend='uia').start("C:\\Program Files (x86)\\Advantech\\AdamApax.NET Utility\\Program\\AdamNET.exe")
    win = app['Advantech Adam/Apax .NET Utility (Win32) Version 2.05.11 (B19)']
    win.wait('ready')
    win.menu_select("Setup->Refresh Serial and Ethernet")
    win.top_window().print_control_identifiers(filename="file.txt")
    # win.top_window().OKButton.click_input()  ---------This is what I hope to do

else
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

问题陈述

我必须以提升权限运行此应用程序。以上是我的代码。问题是我无法识别从菜单中选择后弹出的窗口(在输出图像中查看)。我需要关上窗户。请原谅线路

win.top_window().print_control_identifiers(filename="file.txt")

这意味着将标识符写入文本文件,因为此代码的结构不会显示输出供我查看。但是,由于没有附加任何内容,我猜 pywinauto 无法识别对话框。

为了更清楚地理解,请查看选择菜单时的图像(输入)。

输入

现在,它会弹出这个对话框(输出)

输出

我还使用 spy 来识别标题,它给出:

句柄: 004E07D4, 标题:信息, 类别: #32770(对话), 样式: 94C801C5)

我尝试过的其他事情:

除了win.topwindow() 用于识别对话框之外,我还使用了

win[Information].OKButton.click_input()
win[Information].OK.click_input()
win[Information].OK.close()
win[Information].OK.kill(soft=false)
win.Information.OKButton.click_input()
win.Information.OK.click_input()
win.Information.OK.close()
win.Information.OK.kill(soft=false)
app[Information] ...... curious if I could discover the new window from original application

我还发送了诸如 enter、space、esc 和 alt-f4 之类的键来关闭带有诸如键盘、pynput 和 ctypes 之类的库的对话框。它仍然不起作用。

下载相同应用程序的链接:http: //downloadt.advantech.com/download/downloadsr.aspx ?File_Id=1-1NHAMZX

任何帮助将不胜感激 !

标签: pythonpython-3.xpywinauto

解决方案


我终于找到了一个线程,它展示了多线程解决这个问题的方式。我自己尝试过,它有效。它有点不同,因为代码的一些部分已经贬值了。这是解决方案的链接: 如何阻止警告对话框停止执行控制它的 Python 程序?

以下是我为解决问题所做的编辑:

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():

    def __init__(self, window_name, quit_event):
        threading.Thread.__init__(self)
        self.quit_event = quit_event
        self.window_name = window_name

    def run(self):
        while True:
            try:
                handles = windows.find_windows(title=self.window_name)
            except windows.WindowNotFoundError:
                pass 
            else: 
                for hwnd in handles:
                    app = Application()
                    app.connect(handle=hwnd)
                    popup = app[self.window_name]
                    popup.close()
            if self.quit_event.is_set():
                break
            time.sleep(1) 

    quit_event = threading.Event()
    mythread = ClearPopupThread('Information', quit_event)
    mythread.start()
    application = Application(backend="uia").start("C:\\Program Files (x86)\\Advantech\\AdamApax.NET Utility\\Program\\AdamNET.exe")
    time.sleep(2)
    win = application['Advantech Adam/Apax .NET Utility (Win32) Version 2.05.11 (B19)']
    win.menu_select("Setup->Refresh Serial and Ethernet")
    quit_event.set()

else:
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

最好的事情是这个解决方案适用于所有其他使主脚本停止工作的对话框,我可以通过添加更多多线程来使用它们来执行不同的操作,例如单击按钮、插入值。


推荐阅读