Jump to content
WoodGrain

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
ControlClick($hWnd, "", "[Class:Button;Instance:2]")

Idk what instance the No button really is but since there's only two buttons on there, I'm gonna guess it's 2. If you put the finder tool over it (Autoit Window info tool) you can see what instance it is.

Share this post


Link to post
Share on other sites

Thanks Inuno, that looks like what I need.

I don't have access to the dialogue as it tends to run when I'm away from my computer, but I could write a log file of when it happens that way I'll know if it's working or not.

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

ooh, that's cool, I will definitely use that! Thanks!
I'll spend some time looking through the RegExp code too, looks like exactly what is needed for dealing with a button with a changing value, cheers.

Share this post


Link to post
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

  • Similar Content

    • By _leo_
      Hey there!
      I am having a problem with clicking a button in IE. As you can see in the code, there would be a class name to click it, but since this button exists several times with exactly the same code, I can only use the "Noch nicht veröffentlicht" to separate it from the others. But this part is just pure text without any classification. 
      "<div class="checkmark" data-v-1e5b1f70=""><svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 16 16" data-v-1e5b1f70=""><circle cx="8" cy="8" r="8" data-v-1e5b1f70="" /></svg></div>Noch nicht veröffentlicht"
      I was trying to click it with:
      #include <IE.au3> local $oNNVs = _IEFormGetCollection($oIE2) For $oNNV in $oNNVs If String($oNNV.innertext) = "Noch nicht veröffentlicht" Then _IEAction($oNNV, "click") EndIf Next and:
      $inputs = $oIE2.document.getElementsByTagName("div") For $input In $inputs If $input.innertext == "Noch nicht veröffentlicht" then $input.click() EndIf Next But nothing seems to work. Thanks for the help!
    • By nacerbaaziz
      hello sirs, please i created a tool witch get the focused control in a window and play a audio file linked with this controls
      e.g buttons, checkBoxes, radios, comboboxes, and others
      i know that their is a function that give us the control focus but it return the classNN
      i want to get the class name to use it with a switch and
      because their are more than class e.g button tbutton timagebutton tnewButton...
      please can any one help me to get the class name not the classnn
      thanks in advance
    • By GillesMaisonneuve
      Good morning,
       
      I am trying to align a two-line text in a button so that it would be centered in the button but can't succeed to do it for the vertical part.
      Can someone tell me what I do wrong ?
      Here is my code:
      $btn3 = GUICtrlCreateButton("Execute " & @LF & " file 1", 100, 65, 60, 50, _ BitOr($BS_CENTER,$BS_BOTTOM,$BS_MULTILINE)) and here is the resulting layout I get in my window.
      I tried to follow the help, made a lot of different parameter combinations (here it's `$BS_BOTTOM`, but I tried `$BS_VCENTER` and `$BS_TOP`), still get the same result : vertical alignement is a no go for me.
      Can someone help ?
      TIA
      Gilles

    • By Pike
      Hey Everyone, I haven't been here in a while and recently picked up my old project. Still an FNG!
      To the point: I want to be able to push the button and either play an mp3, open a file, or open an exe program (all with the same button). The problem is that I don't know how to code for that at "case 1"
              Case 1                                                                                                                                             
                  Local $sRead = IniRead("config.ini", "Config", "One", "Default")                                
                  SoundPlay($sRead, @HotKeyPressed)                                                                     <-------------------------------
                  Run($sRead, @HotKeyPressed)                                                                                 <------------------------------- This 'Run' doesn't work even by itself!
      See code below for layout. If you need more information I will attach more code or answer any questions. Any help is greatly appreciated.
       
      Much Respect,
      Pike
       
      Func _HandleButton() $iClickCount += 1 $iTimer = TimerInit() AdlibRegister("CheckButtonPress", $CheckTime) EndFunc Func CheckButtonPress() If TimerDiff($iTimer) < $CheckTime Or $iTimer = 0 Then Return Switch $iClickCount Case 1 Local $sRead = IniRead("config.ini", "Config", "One", "Default") SoundPlay($sRead, @HotKeyPressed) Run($sRead, @HotKeyPressed) Case 2 MsgBox(0, "INFORMATION:", "Choose the file you want designated to this button") Local $sFileOpenDialog = FileOpenDialog(0, @ScriptDir & "\", "All (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.") EndIf Local $sFileName = InputBox("INFORMATION:", "Please Name Your File?" & @CRLF _ & @CRLF _ & "6 Characters or Less", "") IniWrite(@ScriptDir & "\config.ini", "Config", "One", $sFileOpenDialog) IniWrite(@ScriptDir & "\config.ini", "Config", "NameOne", $sFileName) EndSwitch $iClickCount = 0 AdlibUnRegister() EndFunc  

    • By nacerbaaziz
      Hello my friends
      Can we create a single context menu on more than one item?
      For example, a context menu includes standardized options on more than one control, such as buttons or check boxes
      to Create a context menu on one item am using this function
      GUICtrlCreateContextMenu ($ HWND)
      How to link it with more than one element please?
      or if we can't do that, please give me a solution
      so i tried to add an context menus to all the controls but the script will be long, for that if their are any solutions i hope to give it to me
      thanks in advanced
×
×
  • Create New...