Python pywinauto部分标题搜索窗口

4

有没有办法让pywinauto通过部分窗口标题来查找窗口?

这是我的代码:

import pywinauto

pwa_app = pywinauto.application.Application()
w_handle = pywinauto.findwindows.find_windows(title=u'Minitab Professional 5.1 64bit - 3333348.temp.project',
                                              class_name='Window')[0]

问题在于每次打开软件时,temp.project之前的数字都会改变,因此我无法让pywinauto找到正确的窗口。
5个回答

6

通过查看谷歌代码上的源代码,我发现你可以为标题提供一个正则表达式:

#=========================================================================
def find_windows(class_name = None,
                class_name_re = None,
                parent = None,
                process = None,
                title = None,
                title_re = None,
                top_level_only = True,
                visible_only = True,
                enabled_only = False,
                best_match = None,
                handle = None,
                ctrl_index = None,
                predicate_func = None,
                active_only = False,
                control_id = None,
    ):
    """Find windows based on criteria passed in

    Possible values are:

    * **class_name**  Windows with this window class
    * **class_name_re**  Windows whose class match this regular expression
    * **parent**    Windows that are children of this
    * **process**   Windows running in this process
    * **title**     Windows with this Text
    * **title_re**  Windows whose Text match this regular expression
    * **top_level_only** Top level windows only (default=True)
    * **visible_only**   Visible windows only (default=True)
    * **enabled_only**   Enabled windows only (default=True)
    * **best_match**  Windows with a title similar to this
    * **handle**      The handle of the window to return
    * **ctrl_index**  The index of the child window to return
    * **active_only**  Active windows only (default=False)
    * **control_id**  Windows with this control id
   """

据我所知,pywinauto.findwindows.find_windows(title_re = r'Minitab Professional 5.1 64bit*', class_name='Window')[0] 应该可以正常工作。


应该不是 .* 或类似的吗?它能作为通配符或 Python 正则表达式使用吗? - Fenikso
1
我的正则表达式有点生疏,所以你可能是对的。信任,但要验证。 - lucasg

3

使用best_match,无需正则表达式:

handle = pywinauto.findwindows.find_window(best_match='Minitab')
app = pywinauto.application.Application().connect(handle=handle)

简短来说:

app = pywinauto.application.Application().connect(best_match='Minitab')

2

title_re 是Python正则表达式。在您的情况下,它应该像这样:title_re=u'Minitab Professional 5\.1 64bit - \d+\.temp\.project'

\. 表示点符号,. 表示任何符号。

对于完全功能的对话框包装器(而不是句柄),以下内容更简单:

dlg = pwa_app.Window_(title_re=u'Minitab Professional 5\.1 64bit - \d+\.temp\.project', class_name='Window')

它使用正确的process参数(即 pid)调用find_window,以便您不会被来自多个应用程序实例的许多相似窗口所困扰。

顺便说一句,对于64位应用程序,您需要64位兼容的pywinauto克隆版本(官方0.4.2版本仅支持32位Python和应用程序,因为WinAPI结构对齐不同)。


其中一个克隆(如果有的话):https://github.com/vasily-v-ryabov/pywinauto-64 目前正在准备发布。单元测试通过率约为80%,包括Python 3.4。 - Vasily Ryabov
1
新项目页面在这里:http://pywinauto.github.io/ 移动到GitHub正在进行中。 - Vasily Ryabov

2
在这种情况下,最好通过路径连接到应用程序,例如:
app = application.Application(backend="uia")
app.connect(path = r"C:/Program Files/iTunes/iTunes.exe")

0
这是另一种使用pyautogui的解决方案: 我们将通过部分标题找到一个窗口,然后关闭它。
import pyautogui
win = [w for w in pyautogui.getAllWindows() if 'your window partial title' in w.title]
if len(win)>0:
    win[0].close()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接