Python pywinauto搜索窗口与部分标题

时间:2015-01-29 13:41:38

标签: python pywinauto

有没有办法让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 个答案:

答案 0 :(得分:3)

通过浏览google code上的源代码,我看到您可以为标题提供正则表达式:

#=========================================================================
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]应该有效。

答案 1 :(得分:0)

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 param调用process(这是pid),因此您不会对来自多个应用实例的许多类似窗口感到困惑。

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

答案 2 :(得分:0)

在这种情况下,最好通过路径连接到App,例如:

app = application.Application(backend="uia")
app.connect(path = r"C:/Program Files/iTunes/iTunes.exe")

答案 3 :(得分:0)

使用 class OTPVerificationViewController: UIViewController { var verifyid : String = "" let userdefault = UserDefaults() @IBOutlet weak var OTPVerifyCode: UITextField! override func viewDidLoad() { super.viewDidLoad() let valust = self.userdefault.dictionary(forKey: "verifyID") print(verifyid) } @IBAction func verificationSubmitButton(_ sender: Any) { guard let verifycode = OTPVerifyCode.text else {return} let credentials = PhoneAuthProvider.provider().credential(withVerificationID: verifyid, verificationCode: verifycode) Auth.auth().signInAndRetrieveData(with: credentials) { (result, error) in if error == nil { if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "ServiceViewControllerID") as? ServiceViewController { UIApplication.shared.keyWindow?.rootViewController = viewController self.dismiss(animated: true, completion: nil) } } else{ print(error?.localizedDescription) } } } ,不需要正则表达式:

best_match

或更短:

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

答案 4 :(得分: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()