autoit 脚本开发踩坑点

原文

1. 获取不到点击 <input type='file'/> 后弹出的window

根本原因是 _IEAction 阻塞,见第4点
解决办法:

;bad code
$oIE = _IE_Example("form")
$oT = _IEGetObjById($oIE, 'fileExample')
_IEAction($oT,"click")
WinWait("Choose File to Upload") ;等待不到弹出
$hChoose = WinGetHandle("Choose File to Upload")

;good code
$oIE = _IE_Example("form")
$oT = _IEGetObjById($oIE, 'fileExample')
MouseMove(_IEPropertyGet($oT, "screenx") + _IEPropertyGet($oT, "width") - 10, _
          _IEPropertyGet($oT, "screeny") + _IEPropertyGet($oT, "height")/2)
MouseClick("left")
WinWait("Choose File to Upload")

2. send 需要切换英文输入法

如果没有切换英文输入法,会出现中文输入法候选框
如果能用ControlSend,就不推荐用send,如果非要用send,可以切换输入法为英文再send.

;设置指定窗口为英文输入法
$hWnd = WinGetHandle("[ACTIVE]");$hWnd 为目标窗口句柄,这里设置的是当前活动窗口
$ret = DllCall("user32.dll", "long", "LoadKeyboardLayout", "str", "08040804", "int", 1 + 0)
DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $hWnd, "int", 0x50, "int", 1, "int", $ret[0])        
Send('nh')

3. 有时 IE对象 需要重新获取,以便刷新一下值,否则为null

原因未知

4. _IEAction($btnsave,"click") 会阻塞,直到事件完成

所以使用 _IEAction 触发事件时,如果事件里有 alert 之类的弹窗,程序会一直停留在这一句,导致无法继续。所以即便在后面写了 WinWait 等待弹窗的句子,也无济于事。

推荐使用鼠标光标去点击这个按钮,再 WinWait弹窗

5. 日志中的不解之谜

程序运行时日志里,常有下面的 COM Error ,但没有显示异常的行数。结合前后逻辑也没能分析出问题。

另外 scriptline 总是显示 -1 的原因竟然是,编译成exe后,脚本获取不到行数。行数scriptline只会在开发时使用 f5 调试中有效 😂 。(出自链接1连接2

2021-06-30 14:53:03:AutoItCOM Test , We intercepted a COM Error !
2021-06-30 14:53:03:err.description is: 	
2021-06-30 14:53:03:err.windescription:	发生意外。
2021-06-30 14:53:03:err.number is: 	80020009
2021-06-30 14:53:03:err.lastdllerror is: 	0
2021-06-30 14:53:03:err.scriptline is: 	-1
2021-06-30 14:53:03:err.source is: 	
2021-06-30 14:53:03:err.helpfile is: 	
2021-06-30 14:53:03:err.helpcontext is: 	0
2021-06-30 14:53:03:AutoItCOM Test , We intercepted a COM Error !
2021-06-30 14:53:03:err.description is: 	
2021-06-30 14:53:03:err.windescription:	发生意外。
2021-06-30 14:53:03:err.number is: 	80020009
2021-06-30 14:53:03:err.lastdllerror is: 	0
2021-06-30 14:53:03:err.scriptline is: 	-1
2021-06-30 14:53:03:err.source is: 	
2021-06-30 14:53:03:err.helpfile is: 	
2021-06-30 14:53:03:err.helpcontext is: 	0

6. ControlListView 闪退

下面的代码作用是寻找ListView控制中的CutePDF Writer的索引,运行时闪退

Local $index = ControlListView($printWnd, '', 'SysListView321', 'FindItem','CutePDF Writer')

原因是操作64位程序,得编译成64位的脚本
确保你你使用的是完整版 SciTE4AutoIt3.exe,否则下面的声明可能不支持

;脚本开头增加声明
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
posted @ 2022-01-03 13:28  ShaynChow  阅读(228)  评论(0编辑  收藏  举报