python-3.x - 如何使用pywinauto在子窗口中获取类名?
问题描述
我正在尝试使用 pywinauto 自动化应用程序。当我需要定位特定窗口时,这是一个问题。
例如,我想获取窗口“AfxWnd4214”,但我只能访问窗口“AfxWnd42”。
| AfxWnd42 - '' (L1044, T410, R1060, B426)
| ['AfxWnd4214', 'V11.82AfxWnd4212']
| child_window(class_name="AfxWnd42")
| AfxWnd42 - '' (L1044, T410, R1060, B426)
| ['AfxWnd4212', 'V11.82AfxWnd4212']
| child_window(class_name="AfxWnd42")
我通过此代码访问它。但不够具体。此窗口的顺序可能会在树中发生变化。
pywinauto.app.window(class_name = "AfxWnd42", found_index = 0)
当我尝试这段代码时:
pywinauto.app.window(class_name = "AfxWnd4214", found_index = 0)
它说
pywinauto.findwindows.ElementNotFoundError: {'class_name': 'AfxWnd4214', 'found_index': 0, 'backend': 'win32', 'process': 15404}
有没有更好的方法来访问这个窗口?
谢谢你。
解决方案
有各种搜索条件见下文;尝试使用这些的组合。一旦我使用了 auto_id,它就对我有用。
''' 根据传入的条件查找元素
Possible values are:
* **class_name** Elements with this window class
* **class_name_re** Elements whose class matches this regular expression
* **parent** Elements that are children of this
* **process** Elements running in this process
* **title** Elements with this text
* **title_re** Elements whose text matches this regular expression
* **top_level_only** Top level elements only (default=True)
* **visible_only** Visible elements only (default=True)
* **enabled_only** Enabled elements only (default=False)
* **best_match** Elements with a title similar to this
* **handle** The handle of the element to return
* **ctrl_index** The index of the child element to return
* **found_index** The index of the filtered out child element to return
* **predicate_func** A user provided hook for a custom element validation
* **active_only** Active elements only (default=False)
* **control_id** Elements with this control id
* **control_type** Elements with this control type (string; for UIAutomation elements)
* **auto_id** Elements with this automation id (for UIAutomation elements)
* **framework_id** Elements with this framework id (for UIAutomation elements)
* **backend** Back-end name to use while searching (default=None means current active backend)
'''