Jump to content
WoodGrain

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

Recommended Posts

WoodGrain

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
alien4u

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
InunoTaishou
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
WoodGrain

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
InunoTaishou

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
WoodGrain

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

    • TheDcoder
      By TheDcoder
      Hello everyone, long time since I made a post in the H&S sub-forum
      I am working on a project where in a situation I would have to hold down an arrow key and release it in another statement/command, however when I wrote the code it isn't working exactly like I thought it would. Here is an example:
      Send("{UP down}") Sleep(10000) Send("{UP up}") This should hold the up arrow key for 10 seconds before releasing it again, but when I run this code the cursor in SciTE moves up only once
      If I hold the up arrow manually for 10 seconds it "repeats" and the cursor moves up several times until I release the key... Is this a known limitation of Send? Is there any other way I can accomplish this?
      Thanks for all the help and feedback in advance! TD
    • kcvinu
      By kcvinu
      Hi all,
      I have a button created with _GUICtrlButton_Create function. I know that this function returns a handle of button instead of an ID. So i have obtain control ID by calling _WinAPI_GetDlgCtrlID function. After that i have tried the GUICtrlSetBkColor function. But it didnt worked. Any suggestions will be appreciated. Thanks in advance. 
      Note : - The window which carries this button is not created with native gui create function. Instead, it is created with CreateWindowEx api function.
    • kcvinu
      By kcvinu
      Hi all,
      I am playing with _GUICtrlButton_Create function. How can i change this button's (or the entire form's) font ?. The in-built GUICtrlSetFont function is not working even when i convert the control handle to control ID with _WinAPI_GetDlgCtrlID ( ) function.  Do i need to use CreateFont api finction and send WM_SETFONT message ? Or is there any other easy and safe ways to do this ?. Thanks in advance.
      Note : This window is created by CreateWindowEx function, not by GUICreate function. 
    • dadalt
      By dadalt
      Hi!
      I have a button where I need to close it!
      I was doing through  clicking same position in screen but there are some id's that have different sizes.
      What are the possible ways to click this closable button?
      Is there a way to close it through id? Is there a way to get it's position through it's ID?  
      Thanks in advance!


    • Robdog1955
      By Robdog1955
      I'm trying to click a button on a web page. I have added a couple of MsgBox lines to allow me to watch what happens on the page. As you can see the first half of my script enters data into text boxes on the page. I have no problem there. I just cannot click on the region buttons. The "set focus" line causes an outline to appear around the EU button and the "click button" line causes the "Pick a Region" text to disappear. Here is the code I have so far.
      #include <IE.au3> Local $oIE = _IECreate("http://questchecker.com/") Local $iQuestID = "123456" Local $sCharacterName = "CharacterName" Local $colForms = _IEFormGetCollection($oIE) $iCount = 0 For $oForm In $colForms $oFormElements = _IEFormElementGetCollection($oForm) For $oFormElement In $oFormElements $iCount = $iCount + 1 Local $sTagName = StringLower($oFormElement.tagName) Local $sElementType = $oFormElement.type Local $sElementName = $oFormElement.name Switch $iCount Case 6 _IEFormElementSetValue($oFormElement, "MyRealm", 0) ; realm Case 7 _IEFormElementSetValue($oFormElement, $sCharacterName, 0) Case 8 _IEFormElementSetValue($oFormElement, $iQuestID, 0) EndSwitch Next Next Local $oButtons = _IEGetObjByName($oIE, "questForm") For $oButton In $oButtons If _IEFormElementGetValue($oButton) = "US" Then MsgBox(0, "", "Click Okay to set focus") _IEAction($oButton, "focus") MsgBox(0, "", "Click Okay to click button") _IEAction($oButton, "click") ExitLoop EndIf Next MsgBox(0, "", "Click Okay to quit") _IEQuit($oIE) Exit  
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.