zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python操作Windows

PythonWindows 操作
2023-09-11 14:22:10 时间

用python进行windows端UI自动化的库有很多,比如pywinauto等,本文介绍一个使用autoit3来实现的 pyautoit 库

pyautoit 是一个用python写的基于AutoItX3.dll的接口库,用来进行windows窗口的一系列操作,也支持鼠标键盘的操作。

安装

pip install pyautoit

import autoit # 注意:引用的模块名跟安装的模块名是不一样的

启动程序

autoit.run("notepad.exe")

窗口操作

autoit.win_wait_active(title="无标题- 记事本", timeout=10) # 等待窗口激活

autoit.win_exists("aaa") # 判断窗口是否存在

autoit.win_get_handle("无标题- 记事本") # 获取窗口句柄

autoit.win_activate("bbb") # 激活窗口

autoit.win_close("[CLASS:Notepad]") # 关闭窗口

此处,窗口标题的匹配模式也是可以自定义的,默认是1 -- 匹配开始部分,可以在脚本前面加上以下改为2--匹配子字符串:

autoit.opt("WinTitleMatchMode", 2)

控件操作

autoit.control_set_text("无标题", "Edit1", "12312313123") # 输入文字

autoit.control_click(title="title", control="controlid") # 点击

autoit.control_get_text(title="title", control="controlid") # 获取控件文本

autoit.control_command(title="title", control="controlid", command="command")

autoit.control_list_view(title="title", control="controlid", command="command")

autoit.control_tree_view(title="title", control="controlid", command="command")

进程操作

autoit.process.process_wait(process="process", timeout=10)

autoit.process.process_close("process")

autoit.process.process_exists("process")

鼠标操作

autoit.mouse_click(button='left', x=100, y=200, clicks=1, speed=-1)

autoit.mouse_move(x=200, y=300, speed=-1)

autoit.mouse_wheel(direction="down")

键盘操作

autoit.send("12312412")

autoit.send("+{F10}") # 发送shift+F10(右键效果)

autoit.send("^a") # 发送ctrl+a

其他

autoit.shutdown(2)

0 = Logoff 1 = Shutdown 2 = Reboot 4 = Force 8 = Power down