Python pywinauto search windows with partial title

19,318

Solution 1

By skimming the source code on google code, I see you can feed a regex for the title :

#=========================================================================
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
   """

According to me pywinauto.findwindows.find_windows(title_re = r'Minitab Professional 5.1 64bit*', class_name='Window')[0] should work.

Solution 2

title_re works as Python regular expression. In your case it should be like title_re=u'Minitab Professional 5\.1 64bit - \d+\.temp\.project'.

\. means dot symbol, . means any symbol.

For fully functional dialog wrapper (instead of handle) the following thing is simpler:

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

It calls find_window with proper process param (this is pid), so you will not be confused by many similar windows from several app instances.

BTW, for 64-bit application you need 64-bit compatible clone of pywinauto (official 0.4.2 supports only 32-bit Python and apps because of different WinAPI structures alignment).

Solution 3

In this case it's better to connect to App by path, like:

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

Solution 4

Use best_match, no need for regex:

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

or shorter:

app = pywinauto.application.Application().connect(best_match='Minitab')
Share:
19,318
Tiago São José
Author by

Tiago São José

Updated on June 12, 2022

Comments

  • Tiago São José
    Tiago São José almost 2 years

    Is there any way to make pywinauto find a window just with a part of the title?

    This is my code:

    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]
    

    The problem is that the number before temp.project changes every time I open the software and because of that I cannot get pywinauto to find the right window.

  • Fenikso
    Fenikso over 9 years
    Should not it be .* or something similar? Does that work as a wildcard or Python regular expression?
  • lucasg
    lucasg over 9 years
    my regex is a bit rusted, so you're probably right. Trust, but verify.
  • Vasily Ryabov
    Vasily Ryabov over 9 years
    One of the clones (if any): github.com/vasily-v-ryabov/pywinauto-64 Currently preparing for release. Unit tests pass rate is about 80% including Python 3.4.
  • Vasily Ryabov
    Vasily Ryabov almost 9 years
    New project page is here: pywinauto.github.io Moving to GitHub is in progress.