WindowsAppEnv
WindowsAppEnv
类的使用场景如下:
- 打开指定文档。
- 使用不同的策略(
contains
、fuzzy
和regex
)匹配文档窗口。 - 使用各种策略(
contains
、fuzzy
和regex
)匹配实例化计划中每个步骤所需的控件。 - 关闭指定文档。
以下章节详细解释了窗口和控件的匹配策略及其使用方法。
匹配策略
在 WindowsAppEnv
类中,匹配策略是确定如何将 window
或 control
名称与给定文档名称或目标文本匹配的规则。根据配置文件,可以选择三种不同的匹配策略:contains
、fuzzy
和 regex
。
Contains
匹配是最简单的策略,适用于窗口和文档名称完全匹配的情况。Fuzzy
匹配更灵活,即使窗口标题和文档名称之间存在拼写错误或部分匹配,也能进行匹配。Regex
匹配提供了最大的灵活性,非常适合窗口标题中复杂的匹配模式。
1. 窗口匹配示例
find_matching_window
方法负责根据配置的匹配策略匹配窗口。以下是您可以使用它通过提供文档名称来查找窗口的方法:
示例
# Initialize your application object (assuming app_object is already defined)
app_env = WindowsAppEnv(app_object)
# Define the document name you're looking for
doc_name = "example_document_name"
# Call find_matching_window to find the window that matches the document name
matching_window = app_env.find_matching_window(doc_name)
if matching_window:
print(f"Found matching window: {matching_window.element_info.name}")
else:
print("No matching window found.")
解释
app_env.find_matching_window(doc_name)
将搜索所有打开的窗口,并使用配置中定义的策略(contains、fuzzy 或 regex)匹配窗口标题。- 如果找到匹配项,
matching_window
对象将包含匹配的窗口,您可以打印窗口的名称。 - 如果未找到匹配项,它将返回
None
。
2. 控件匹配示例
要在窗口中查找匹配的控件,您可以使用 find_matching_controller
方法。此方法需要一个过滤后的控件字典和一个要匹配的控件文本。
示例
# Initialize your application object (assuming app_object is already defined)
app_env = WindowsAppEnv(app_object)
# Define a filtered annotation dictionary of controls (control_key, control_object)
# Here, we assume you have a dictionary of UIAWrapper controls from a window.
filtered_annotation_dict = {
1: some_control_1, # Example control objects
2: some_control_2, # Example control objects
}
# Define the control text you're searching for
control_text = "submit_button"
# Call find_matching_controller to find the best match
controller_key, control_selected = app_env.find_matching_controller(filtered_annotation_dict, control_text)
if control_selected:
print(f"Found matching control with key {controller_key}: {control_selected.window_text()}")
else:
print("No matching control found.")
解释
filtered_annotation_dict
是一个字典,其中键表示控件的 ID,值是控件对象 (UIAWrapper
)。control_text
是您在这些控件中搜索的文本。app_env.find_matching_controller(filtered_annotation_dict, control_text)
将根据定义的策略计算每个控件的匹配分数,并返回匹配分数最高的控件。- 如果找到匹配项,它将返回控件对象 (
control_selected
) 及其键 (controller_key
),可用于进一步交互。
参考
表示 Windows 应用程序环境。
初始化 Windows 应用程序环境。
参数 |
|
---|
源代码位于 env/env_manager.py
29 30 31 32 33 34 35 36 37 38 |
|
close()
尝试正常关闭应用程序;如果失败或未关闭,则强制终止进程。
源代码位于 env/env_manager.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
find_matching_controller(filtered_annotation_dict, control_text)
" 选择最佳匹配的控制器。
参数 |
|
---|
返回 |
|
---|
源代码位于 env/env_manager.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
find_matching_window(doc_name)
根据进程名称和配置的匹配策略查找匹配的窗口。
参数 |
|
---|
返回 |
|
---|
源代码位于 env/env_manager.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
start(copied_template_path)
启动 Windows 环境。
参数 |
|
---|
源代码位于 env/env_manager.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|