GUI 自动化器
GUI 自动化器能够模拟鼠标和键盘在应用程序 UI 控件上的操作。UFO 使用 UIA 或 Win32 API 与应用程序的 UI 控件(如按钮、编辑框和菜单)进行交互。
配置
在使用 UI 自动化器之前,需要先在 config_dev.yaml
文件中设置一些配置。下面是与 UI 自动化器相关的配置列表
配置选项 | 描述 | 类型 | 默认值 |
---|---|---|---|
CONTROL_BACKEND |
控制操作的后端列表,目前支持 uia 、win32 和 onmiparser |
列表 | ["uia"] |
CONTROL_LIST |
允许选择的小部件列表。 | 列表 | ["Button", "Edit", "TabItem", "Document", "ListItem", "MenuItem", "ScrollBar", "TreeItem", "Hyperlink", "ComboBox", "RadioButton", "DataItem"] |
ANNOTATION_COLORS |
分配给不同控件类型用于注释的颜色。 | 字典 | {"Button": "#FFF68F", "Edit": "#A5F0B5", "TabItem": "#A5E7F0", "Document": "#FFD18A", "ListItem": "#D9C3FE", "MenuItem": "#E7FEC3", "ScrollBar": "#FEC3F8", "TreeItem": "#D6D6D6", "Hyperlink": "#91FFEB", "ComboBox": "#D8B6D4"} |
API_PROMPT |
用于 UI 自动化 API 的提示词。 | 字符串 | "ufo/prompts/share/base/api.yaml" |
CLICK_API |
用于点击操作的 API,可以是 click_input 或 click 。 |
字符串 | "click_input" |
INPUT_TEXT_API |
用于输入文本操作的 API,可以是 type_keys 或 set_text 。 |
字符串 | "type_keys" |
INPUT_TEXT_ENTER |
在输入文本后是否按回车键。 | 布尔值 | False |
接收器
UI 自动化器的接收器是定义在 ufo/automator/ui_control/controller/control_receiver
模块中的 ControlReceiver
类。它使用应用程序的窗口句柄和执行操作的控件包装器进行初始化。ControlReceiver
提供了与应用程序 UI 控件交互的功能。下面是 ControlReceiver
类的参考
基类:ReceiverBasic
控制接收器类。
初始化控制接收器。
参数 |
|
---|
源代码在 automator/ui_control/controller.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
annotation(params, annotation_dict)
截取当前应用程序窗口的屏幕截图,并在屏幕截图上注释控制项。
参数 |
|
---|
源代码在 automator/ui_control/controller.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
atomic_execution(method_name, params)
在控制元素上原子执行操作。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
click_input(params)
点击控制元素。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
click_on_coordinates(params)
点击控制元素的坐标。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
drag_on_coordinates(params)
拖动控制元素的坐标。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
key_press(params)
在控制元素上按键。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
keyboard_input(params)
在控制元素上进行键盘输入。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
mouse_move(params)
在控制元素上移动鼠标。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
no_action()
不对控制元素执行任何操作。
返回 |
|
---|
源代码在 automator/ui_control/controller.py
316 317 318 319 320 321 322 |
|
scroll(params)
在控制元素上滚动。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
set_edit_text(params)
设置控制元素的编辑文本。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
summary(params)
控制元素的视觉摘要。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
156 157 158 159 160 161 162 163 |
|
texts()
获取控制元素的文本。
返回 |
|
---|
源代码在 automator/ui_control/controller.py
253 254 255 256 257 258 |
|
transform_point(fraction_x, fraction_y)
将相对坐标转换为绝对坐标。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
transform_scaled_point_to_raw(scaled_x, scaled_y, scaled_width, scaled_height, raw_width, raw_height)
将缩放坐标转换为原始坐标。
参数 |
|
---|
源代码在 automator/ui_control/controller.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|
transfrom_absolute_point_to_fractional(x, y)
将绝对坐标转换为相对坐标。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
type(params)
在控制元素上键入。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
306 307 308 309 310 311 312 313 314 |
|
wait_enabled(timeout=10, retry_interval=0.5)
等待直到控件启用。
参数 |
|
---|
源代码在 automator/ui_control/controller.py
340 341 342 343 344 345 346 347 348 349 350 351 |
|
wait_visible(timeout=10, retry_interval=0.5)
等待直到窗口可见。
参数 |
|
---|
源代码在 automator/ui_control/controller.py
353 354 355 356 357 358 359 360 361 362 363 364 |
|
wheel_mouse_input(params)
在控制元素上进行鼠标滚轮输入。
参数 |
|
---|
返回 |
|
---|
源代码在 automator/ui_control/controller.py
260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
命令
UI 自动化器的命令是定义在 ufo/automator/ui_control/controller/ControlCommand
模块中的 ControlCommand
类。它封装了执行操作所需的函数和参数。ControlCommand
类是 UI 自动化器应用程序中所有命令的基类。下面是一个继承自 ControlCommand
类的 ClickInputCommand
类的示例
@ControlReceiver.register
class ClickInputCommand(ControlCommand):
"""
The click input command class.
"""
def execute(self) -> str:
"""
Execute the click input command.
:return: The result of the click input command.
"""
return self.receiver.click_input(self.params)
@classmethod
def name(cls) -> str:
"""
Get the name of the atomic command.
:return: The name of the atomic command.
"""
return "click_input"
注意
具体的命令类必须实现 execute
方法来执行操作,以及 name
方法来返回原子命令的名称。
注意
每个命令都必须使用 @ControlReceiver.register
装饰器注册到特定的 ControlReceiver
才能执行。
下面是 UFO 目前支持的 UI 自动化器中可用的命令列表
命令名称 | 函数名称 | 描述 |
---|---|---|
ClickInputCommand |
click_input |
用鼠标点击控制项。 |
ClickOnCoordinatesCommand |
click_on_coordinates |
点击应用程序窗口的特定分数坐标。 |
DragOnCoordinatesCommand |
drag_on_coordinates |
在应用程序窗口的特定分数坐标上拖动鼠标。 |
SetEditTextCommand |
set_edit_text |
向控制项添加新文本。 |
GetTextsCommand |
texts |
获取控制项的文本。 |
WheelMouseInputCommand |
wheel_mouse_input |
滚动控制项。 |
KeyboardInputCommand |
keyboard_input |
模拟键盘输入。 |
提示
请参阅 ufo/prompts/share/base/api.yaml
文件以获取 UI 自动化器的详细 API 文档。
提示
您可以通过向 ufo/automator/ui_control/controller/ControlCommand
模块添加新的命令类来定制命令。