Jump to content

How do I Click a button with ControlSend or ControlClick with a variable button name?


Recommended Posts

Hi All,

I'm wanting to learn how to use ControlSend or ControlClick to be able to click a button when prompted, part of the problem is the button text changes.
The button I want to click displays "NO (XX)" where XX is a countdown each second from 90 - image attached.

I've not done Control based code before, so the below code may be wrong lol, but this is what I've got so far:

While 1
   If WinExists("System", "reboot") Then
      Local $hWnd = WinWait("System", "reboot", 5)
      Local $iPID = WinGetProcess($hWnd)
      ControlClick($hWnd, "", "[CLASS:Button; Text: NO"]
      ;ProcessClose($iPID)
   EndIf
   Sleep(10000)
WEnd

Thanks!

Reboot Countdown.jpg

Link to comment
Share on other sites

What I do when I can't directly send to the specific control is use TAB and ENTER, like this:

WinWait ("[CLASS:#32770]")
$id = WinGetHandle("[CLASS:#32770]")
ControlSend($id, "", "","{TAB 3}")
ControlSend($id, "", "","{ENTER}")


Maybe this work for you, but I'm almost sure there is a better way to do it and the ones with more experience will say.

Regards
Alien.

Link to comment
Share on other sites

You could use this to log what instance the button is for the dialog, in case it's not actually 2. Go through every button control on the dialog and if it matches the expression, click it.

#include <Array.au3>

Global $hWnd = WinGetHandle("System", "reboot")
Global $aClassList = StringSplit(WinGetClassList($hWnd), @LF, 2)

For $i = UBound($aClassList) - 1 To 0 Step -1
    If ($aClassList[$i] <> "Button") Then _ArrayDelete($aClassList, $i)
Next

For $i = 1 to UBound($aClassList)
    Local $sButtonText = ControlGetText($hWnd, "", "[Class:Button;Instance:" & $i & "]")
    If (StringRegExp($sButtonText, "(?i)No \(\d{1,2}\)")) Then
        ControlClick($hWnd, "", "[Class:Button;Instance:" & $i & "]")
        ExitLoop
    EndIf
Next

 

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

×
×
  • Create New...