Jump to content

How to click on a button


Go to solution Solved by mistersquirrle,

Recommended Posts

I would like to click on the continue button every time this windows cums up.

Error.jpg.a8de43cd0e0bddf450ea2030c4013319.jpg

This is what AutoIt sees:

ErrorAutoit.thumb.jpg.f94499990ecfcb2df4128a412055cc11.jpg

This is what I have for code that isn't working:

Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number
HotKeySet("{F8}", "Terminate")
    Local $sWindowsTitle = "[CLASS:##32770]" ;Set The Window title Here

While 1
    If WinExists("Error Applying Security", "An error occurred while applying security information") Then
        WinActivate("Error Applying Security", "An error occurred while applying security information")
        WinWaitActive("Error Applying Security", "An error occurred while applying security information")
        ControlSend("Error Applying Security", "&Continue", "1", "{ENTER}")
    EndIf
    Sleep(8000)
WEnd

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

 

Edited by Docfxit
Link to comment
Share on other sites

Try using ControlClick instead of ControlSend. If you want to use ControlSend, you may need to also use ControlFocus first as well. Here's a modification to your script for how I would go about it:

Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number
HotKeySet("{F8}", "Terminate")
Global $sWindowsTitle = "[TITLE:Error Applying Security;CLASS:#32770]" ;Set The Window title Here
Global $sWinText = "An error occurred while applying security information" ; Make sure to check the 'Visible Text' tab in the window info to see if it actually exists on the window/msgbox before you try to use it
Global $hWnd

While 1
    $hWnd = WinGetHandle($sWindowsTitle)
    If Not @error Then ; Alternative method to WinExists, now we have the window handle if it did exist, so we can make sure we're referencing the same one in the other functions
        Do
            WinActivate($hWnd)
            Sleep(100) ; Avoid high CPU usage
        Until WinActive($hWnd) ; Keep trying to activate the window until it is active
        ControlClick($hWnd, '', 'Button1', 'main', 1)
        Sleep(100) ; Give the window some extra time to close after we click the button
    EndIf

    Sleep(100) ; You don't need a high sleep, in most cases Sleep(10) will result in 0% CPU usage (though not here)
WEnd

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Also keep in mind that you're not using any MouseClick or other Mouse functions, however you have MouseCoordMode set to 0, which changes the default behavior of it. The default is 1 - absolute screen coordinates, 0 is relative coords to the active window, however ControlClick is NOT affected by this setting, since everything is control/window based for it.

Edited by mistersquirrle

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

  • Solution

Instead of 'Button1' for the ControlClick, try one of these:

ControlClick($hWnd, '', 'Button', 'main', 1)
ControlClick($hWnd, '', '[CLASS:Button; INSTANCE:1]', 'main', 1)
ControlClick($hWnd, '', 1, 'main', 1)

Alternatively, if it doesn't matter if you click Continue or Cancel you could try just using WinClose or WinKill, though I imagine Continue makes it skip over the current item and continue processing, so those may not be options for you.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Do you know where it's stopping? Is it failing to detect the window? Or failing to click?

If it's failing to find the window, double check the Window Info. Failing to click, double check the Button control information.

Try this, added a little lite logging and relaxed the window matching conditions:

#RequireAdmin
Opt("WinTitleMatchMode", 2) ; 2 - Match any substring in the title
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number

HotKeySet("{F8}", "Terminate")

;~ Global $sWindowsTitle = "[TITLE:Error Applying Security;CLASS:#32770]" ;Set The Window title Here
Global $sWindowsTitle = "Error Applying Security" ;Set The Window title Here
Global $sWinText = "An error occurred while applying security information" ; Make sure to check the 'Visible Text' tab to see if it actually exists
Global $aCtrlButtonsToClick[] = ['[CLASS:Button; INSTANCE:1]', 'Button1', 'Button', 1]
Global $hCtrl
Global $hWnd

While 1
    $hWnd = WinGetHandle($sWindowsTitle)
    If Not @error Then ; Alternative method to WinExists, now we have the window handle if it did exist, so we can make sure we're referencing the same one in the other functions
        __ConsoleLog('Found window: ' & $hWnd & ', title: ' & WinGetTitle($hWnd) & ', text: ' & WinGetText($hWnd))
        Do
            __ConsoleLog('Activating window')
            WinActivate($hWnd)
            Sleep(100) ; Avoid high CPU usage
        Until WinActive($hWnd) Or Not WinExists($hWnd) ; Keep trying to activate the window until it is active

        __ConsoleLog('Attempting to click button')
        While WinExists($hWnd)
            For $iButton = 0 To UBound($aCtrlButtonsToClick) - 1
                $hCtrl = ControlGetHandle($hWnd, '', $aCtrlButtonsToClick[$iButton])
                If Not @error Then
                    ControlFocus($hWnd, '', $hCtrl)
                    __ConsoleLog('ControlClick (' & $aCtrlButtonsToClick[$iButton] & '; Hnd: ' & $hCtrl & ') result: ' & ControlClick($hWnd, '', $hCtrl, 'main', 1))
                EndIf
            Next
            Sleep(250)
        WEnd

        Sleep(250) ; Give the window some extra time to close after we click the button
    EndIf

    Sleep(100) ; You don't need a high sleep, in most cases Sleep(10) will result in 0% CPU usage (though not here)
WEnd

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Func __ConsoleLog($sMsg)
    Local Static $sPrevMsg = ''

    If $sPrevMsg == $sMsg Then Return True

    ConsoleWrite($sMsg & @CRLF)

    $sPrevMsg = $sMsg

    Return True
EndFunc   ;==>__ConsoleLog

 

Edit: Glad it's working, upped the sleep in this updated example

Edited by mistersquirrle
Updated code

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...