【问题标题】:Issues while automating button click using AutoIT使用 AutoIT 自动单击按钮时的问题
【发布时间】:2013-09-20 22:50:28
【问题描述】:

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

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

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

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

但是什么也没发生。

【问题讨论】:

  • 您应该考虑先使用$button = ControlGetHandle(...) 来检查您是否以正确的方式寻址控件。值得检查 ControlFocus("Free HTML to PDF Converter", "", $button) 是否与返回的句柄一起使用。由于 NAME-Attribute 是 .Net 特定的,因此您应该尝试替换它。
  • 运行程序后,您必须等待窗口出现,然后才能单击其上的任何按钮。

标签: automation click autoit


【解决方案1】:

使用 AutoIt 自动化 GUI 时,需要牢记几件事。第一种是在你的脚本中使用 AutoItSetOption(),通常在最开始的时候是这样的:

#NoTrayIcon
AutoItSetOption("WinTitleMatchMode", 2)

我将选项 2 用于 WinTitleMatchMode,因为它告诉 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 窗口信息工具。我可以将它的指针拖到我正在尝试使用的元素上,并获取有关该控件的所有详细信息。

【讨论】:

    【解决方案2】:

    我编写了一个 UDF 来满足我的要求,传递了我找到控件或窗口所需的所有信息。示例用法。 CheckClickCtrl ('[CLASS:#32770]', 'ApplicationX - InstallShield Wizard', '[类:按钮; 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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 2021-09-14
    • 2012-05-29
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多