我正在尝试使用 AutoIT 自动单击按钮。我可以使用以下命令运行应用程序。
Run("C:\HtmlToPdf.exe");
Au3Info工具表明我的窗口的标题是Free HTML to PDF Converter
,按钮的名称是btnConvert
。我尝试使用以下命令单击该按钮。
ControlClick("Free HTML to PDF Converter", "", "[NAME:btnConvert]");
但什么也没有发生。
我正在尝试使用 AutoIT 自动单击按钮。我可以使用以下命令运行应用程序。
Run("C:\HtmlToPdf.exe");
Au3Info工具表明我的窗口的标题是Free HTML to PDF Converter
,按钮的名称是btnConvert
。我尝试使用以下命令单击该按钮。
ControlClick("Free HTML to PDF Converter", "", "[NAME:btnConvert]");
但什么也没有发生。
使用 AutoIt 自动化 GUI 时,需要牢记几件事。第一种是在你的脚本中使用 AutoItSetOption() ,通常在一开始就这样:
#NoTrayIcon
AutoItSetOption("WinTitleMatchMode", 2)
我对 WinTitleMatchMode 使用选项 2,因为它告诉 AutoIt 匹配标题中的任何子字符串。接下来运行程序并等待窗口。我将使用我的一个脚本中的一个示例...
#NoTrayIcon
AutoItSetOption("WinTitleMatchMode", 2)
Run("Setup.exe", @scriptdir)
WinWait("Setup")
我对这个程序很幸运,不需要编写太多脚本,但我遇到了安装程序的障碍。在完成一些检查之前,完成按钮不会出现。所以我不得不像这样在某一点捕获脚本:
#NoTrayIcon
AutoItSetOption("WinTitleMatchMode", 2)
Run("Setup.exe", @scriptdir)
WinWait("Setup")
...click...
...click...
Do
$Finish = ControlGetHandle("Setup", "Finish", 1)
Sleep(100)
Until $Finish <> ""
ControlClick("Setup", "Finish", 1)
Exit
我也喜欢使用 AutoIt Window Info Tool。我可以将它的指针拖到我正在尝试使用的元素上,并获取有关该控件的所有详细信息。
我写了一个 UDF 来执行我的命令,传递了我需要找到控件或窗口的所有信息。示例用法。CheckClickCtrl ('[CLASS:#32770]', 'ApplicationX - InstallShield Wizard', '[CLASS:Button; INSTANCE:1]', , '&Next >' 5000, 0)
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Timers.au3>
#include <GUIConstantsEx.au3>
#include "log4a.au3"
#Region ;**** Logging ****
; Enable logging and don't write to stderr
_log4a_SetEnable()
; Write to stderr, set min level to warn, customize message format
_log4a_SetErrorStream()
_log4a_SetMinLevel($LOG4A_LEVEL_TRACE)
; If @compiled Then _log4a_SetMinLevel($LOG4A_LEVEL_WARN) ; Change the min level if the script is compiled
_log4a_SetFormat("${date} | ${host} | ${level} | ${message}")
#EndRegion ;**** Logging ****
Const $TD_BTN_NEXT = '&Next >'
Const $TD_BTN_INSTALL = '&Install'
Const $TD_BTN_CANCEL = 'Cancel'
Const $TD_BTN_FINISH = 'Finish'
Const $TD_BTN_UNINSTALL = '&Uninstall'
Const $TD_BTN_CLOSE = 'Close'
Const $TD_BTN_YES = '&Yes'
Const $formclass = "[CLASS:#32770]"
Const $Edit = "[CLASS:Edit; INSTANCE:1]"
Const $button1 = "[CLASS:Button; INSTANCE:1]"
Const $button2 = "[CLASS:Button; INSTANCE:2]"
Const $button3 = "[CLASS:Button; INSTANCE:3]"
Const $button4 = "[CLASS:Button; INSTANCE:4]"
Const $7zWindowInst = '7-Zip 15.14 (x64) Setup'
Const $7zWindowUnInst = '7-Zip 15.14 (x64) Uninstall'
unInst7z()
;~ inst7z()
Func inst7z()
_log4a_Info('inst7z:Begin')
CheckClickCtrl($formclass, $7zWindowInst, $button2, $TD_BTN_INSTALL, 0, 0)
CheckClickCtrl($formclass, $7zWindowInst, $button2, $TD_BTN_CLOSE, 0, 0)
_log4a_Info('inst7z():End')
EndFunc ;==>inst7z
Func unInst7z()
_log4a_Info('unInst7z:Begin')
CheckClickCtrl($formclass, $7zWindowUnInst, $button1, $TD_BTN_UNINSTALL, 0, 0)
CheckClickCtrl($formclass, $7zWindowUnInst, $button1, $TD_BTN_CLOSE, 0, 0)
_log4a_Info('unInst7z():End')
EndFunc ;==>unInst7z
Func CheckClickCtrl($formclass, $text, $ctrl, $ctrltxt, $timeout, $delayafter)
_log4a_Info("CheckClickCtrl():Begin")
_log4a_Info("Waiting for: " & $formclass & " " & $text & " " & $ctrl & " " & $ctrltxt)
_log4a_Info("Timeout: " & $timeout)
Local $time_run = _Timer_Init()
While (1)
If WinExists($formclass) Then
Local $hCtrl = ControlGetHandle($text, '', $ctrl)
If $hCtrl Then
If ($timeout > 0) Then
_log4a_Trace(_Timer_Diff($time_run))
If (_Timer_Diff($time_run) > $timeout) Then
_log4a_Info("ExitLoop:Timeout - " & $ctrl)
ExitLoop
EndIf
EndIf
Local $hCtrlHandle = ControlGetText($text, '', $ctrl)
_log4a_Info("Control Text : " & $hCtrlHandle)
_log4a_Info("Control Text Search : " & $ctrltxt)
If ($hCtrlHandle == $ctrltxt) Then
; we got the handle, so the button is there
; now do whatever you need to do
_log4a_Info($formclass)
_log4a_Info($ctrl)
ControlClick($formclass, "", $ctrl)
If ($delayafter > 0) Then
Sleep($delayafter)
EndIf
_log4a_Trace("ExitLoop:Normal - " & $ctrl)
ExitLoop
EndIf
EndIf
EndIf
WEnd
_log4a_Info("CheckClickCtrl():End")
EndFunc ;==>CheckClickCtrl
结果如下:
安装结果:
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | inst7zinst7z:Begin
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | CheckClickCtrl():Begin
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Waiting for: [CLASS:#32770] 7-Zip 15.14 (x64) Setup [CLASS:Button; INSTANCE:2] &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Timeout: 0
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text : &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text Search : &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | [CLASS:#32770]
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | [CLASS:Button; INSTANCE:2]
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Trace | ExitLoop:Normal - [CLASS:Button; INSTANCE:2]
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | CheckClickCtrl():End
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | CheckClickCtrl():Begin
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Waiting for: [CLASS:#32770] 7-Zip 15.14 (x64) Setup [CLASS:Button; INSTANCE:2] Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Timeout: 0
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text : &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text : &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text : &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text : &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text : &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text : &Install
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text : Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | [CLASS:#32770]
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | [CLASS:Button; INSTANCE:2]
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Trace | ExitLoop:Normal - [CLASS:Button; INSTANCE:2]
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | CheckClickCtrl():End
10\02\2017 14:32:30 | WIN-4VR4FSF2H8Q | Info | inst7z():End
+>14:32:30 AutoIt3.exe ended.rc:0
+>14:32:30 AutoIt3Wrapper Finish
Uninstall results:
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | unInst7zinst7z:Begin
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | CheckClickCtrl():Begin
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Waiting for: [CLASS:#32770] 7-Zip 15.14 (x64) Uninstall [CLASS:Button; INSTANCE:1] &Uninstall
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Timeout: 0
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text : &Uninstall
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text Search : &Uninstall
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | [CLASS:#32770]
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | [CLASS:Button; INSTANCE:1]
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Trace | ExitLoop:Normal - [CLASS:Button; INSTANCE:1]
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | CheckClickCtrl():End
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | CheckClickCtrl():Begin
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Waiting for: [CLASS:#32770] 7-Zip 15.14 (x64) Uninstall [CLASS:Button; INSTANCE:1] Close
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Timeout: 0
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text : &Uninstall
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text : &Uninstall
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text : &Uninstall
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text : &Uninstall
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text : Close
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | Control Text Search : Close
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | [CLASS:#32770]
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | [CLASS:Button; INSTANCE:1]
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Trace | ExitLoop:Normal - [CLASS:Button; INSTANCE:1]
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | CheckClickCtrl():End
10\02\2017 14:35:00 | WIN-4VR4FSF2H8Q | Info | inst7z():End
+>14:35:00 AutoIt3.exe ended.rc:0
+>14:35:00 AutoIt3Wrapper Finished.
>Exit code: 0 Time: 0.7732