如何在弹出窗口中读取文本以识别它?

2024-06-16 14:04:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试远程控制一个windows应用程序,它有时在启动时使用pywinauto显示警告窗口。在

下面的代码标识窗口,因为它没有菜单。在

我想阅读弹出的文本来查找短语“请联系您的系统管理员。”在弹出窗口中知道它是 没错。在

mywindows = pywinauto.findwindows.find_windows(title_re=".*MyProgramTitle")

# proof that two windows are found
print(len(mywindows))

for handle in mywindows:
    print('\nhandle {}'.format(handle))

    app = Application().connect(handle=handle)
    navwin = app.window(handle=handle )

    if not navwin.menu_items():
        # no menu - I bet it's a pop up
        print('{} is the window I\'m looking for'.format(handle))
        navwin.print_control_identifiers()

上面的代码打印出所有windows信息,包括 “静态-”位置映射失败。请与系统管理员联系。“”

但我需要抓到打印出来的信息来进一步处理。在


Tags: 代码信息appformatfor远程windowswindow
2条回答

作为一个骇人听闻的解决方案,我浏览了 print_control_identifiers()并找到了这种方法来循环访问窗口的控件

navwin.print_control_identifiers()

for x in navwin.descendants():
    print (x.window_text())
    print (x.class_name())

find_windows是自动化的非常低级的入口点。使用WindowsSpecification对象,您可以等待打开所需的对话框/控件,或者只检查它是否存在(都有自定义的超时)。在

请参阅Getting Started Guide中的详细说明。在

您可以使用exists()visible()方法(返回布尔值),而不是wait('exists')或{},如果失败,这些方法会引发异常。在

你的情况可能是这样的:

static = app.DialogName.child_window(title_re='.*Please contact your system administrator.',
                                     class_name_re='Static')
if static.exists(timeout=20): # if it opens no later than 20 sec.
    app.DialogName.OK.click()

相关问题 更多 >