Pywinauto无法找到/关闭弹出窗口

时间:2019-09-27 02:33:55

标签: python python-3.x 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无法识别该对话框。

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

Input

现在,它弹出此对话框(输出)

Output

我还使用间谍来识别标题,它给出了:

句柄: 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键,空格键,esc和alt-f4键,以关闭带有键盘,pynput和ctypes等库的对话框。仍然不起作用。

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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我终于找到了一个演示多线程如何解决此问题的方法的线程。我自己尝试过,并且有效。由于部分代码已贬值,因此有所不同。这是解决方案的链接: How to stop a warning dialog from halting execution of a Python program that's controlling it?

以下是我为解决该问题所做的修改:

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)

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