pythonui-automationpywinautomicrosoft-ui-automation

How to search children/descendants by regular expression title in pywinauto?


I am trying to get children/descendant UI elements of another UI element by a regexp title.

For example, the following code should work.

from pywinauto.application import Application, WindowSpecification
root_ws: WindowSpecification = (
    Application(backend="uia")
        .connect(path="C:/program.exe")
        .window(title_re="^Program *")
)
root_ws.descendants(title_re="^abc*", control_type="DataItem")

However, as described (by Vasily Ryabov) in this comment, title_re is not possible for children/descendants.

The function that supports search of children by a regular expression is find_elements, however it doesn't accept root_ws as parent:

import pywinauto
pywinauto.findwindows.find_elements(title_re="^abc*", 
                                    top_level_only=False,
                                    parent=root_ws)

throws an exception AttributeError: 'StaticWrapper' object has no attribute 'rich_text'

How could I find a child of another UI element by only having a regexp title?


Solution

  • In pywinauto 0.6.8, you can simply use child_window to find an element. It takes the same exact parameters as find_elements.

    Example:

    from pywinauto.application import Application
    
    root_ws = Application(backend="uia").connect(path="C:/program.exe").window(title_re="^Program *")
    root_ws.child_window(title_re="^abc*", control_type="DataItem")