使用AutoIT自动点击按钮时出现问题

时间:2013-09-20 22:50:28

标签: automation click autoit

我正在尝试使用AutoIT自动按钮单击。我可以使用以下命令运行应用程序。

Run("C:\HtmlToPdf.exe");

工具 Au3Info 表示我的窗口标题为Free HTML to PDF Converter,按钮名称为btnConvert。我尝试使用以下命令单击该按钮。

ControlClick("Free HTML to PDF Converter", "", "[NAME:btnConvert]");

但没有任何反应。

2 个答案:

答案 0 :(得分:1)

使用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窗口信息工具。我可以将其指针拖到我正在尝试使用的元素之上,并获取有关该控件的所有详细信息。

答案 1 :(得分:0)

我写了一个UDF来做我的出价,传递我需要的所有信息来找到控件或窗口。用法示例。 CheckClickCtrl(&#39; [CLASS:#32770]&#39;,&#39; ApplicationX - InstallShield Wizard&#39;,                              &#39; [CLASS:按钮;实例:1]&#39;,&#39;&amp; Next&gt;&#39; 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