有什么方法可以让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
找到正确的窗口。
发布于 2015-01-29 13:54:49
通过在谷歌代码上浏览源代码,我看到您可以为标题提供一个正则表达式:
#=========================================================================
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]
应该能工作。
发布于 2019-09-29 10:29:19
使用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')
发布于 2015-01-30 13:56:51
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')
它使用适当的find_window
参数(这是pid)调用process
,因此您不会被来自多个应用程序实例的许多类似的窗口所迷惑。
顺便说一句,对于64位应用程序,您需要64位兼容的pywinauto克隆(官方的0.4.2只支持32位Python和应用程序,因为不同的WinAPI结构对齐方式)。
https://stackoverflow.com/questions/28216222
复制相似问题