在Python中使用AutoIt的方法有两种:
pyautoit模块将使用DLL,而pywin32我们可以使用COM。据我所知,两者之间在功能上没有区别。
并非所有AutoIt功能都可以通过COM / DLL接口使用。若要查看哪些功能,请参阅AutoItX上的帮助文件。
通过pip或您的首选方法安装:
pip install -U pyautoit
如果出现错误:WindowsError: [Error 193] %1 is not a valid Win32 application
安装pyautoit时,使用32位版本的python。我无法使用python 64位版本安装pyautoit。当然,您的里程可能会有所不同。
导入和使用:
import autoit
autoit.run("notepad.exe")
autoit.win_wait_active("[CLASS:Notepad]", 3)
autoit.control_send("[CLASS:Notepad]", "Edit1", "hello world{!}")
autoit.win_close("[CLASS:Notepad]")
autoit.control_click("[Class:#32770]", "Button2")
autoit命令全部使用lower_case_with_underscores,而不是AutoItX的首选CamelCase。因此,ControlSend变为control_send,WinClose变为win_close,依此类推。
安装pywin32后,可通过以下方式调用AutoItX函数:
import win32com.client
autoit = win32com.client.Dispatch("AutoItX3.Control")
autoit.Run("NotePad.exe")
autoit.ControlClick(WINDOW, "", "[CLASSNN:TTreeView1]", "left", 1, 53, 41)
如果您对此版本有疑问,请以32位安装所有内容,然后重试。