Jump to content

ControlClick does not find controls searched by CLASS and TEXT


 Share

Recommended Posts

Hi,

.. well actually it does but if the TEXT of the control contains a leading whitespace it doesn't.

ControlClick($windowname,"","[CLASS:Button; TEXT: About]");

I tried reading the control text after manually looking up the controlId and it really has a leading whitespace.

I also tried

$about=" About";
ControlClick($windowname,"","[CLASS:Button; TEXT:"&$about&"]");

which had the same effect.

So does the implementation of the control search strip this whitespace or am I doing something wrong?

help / info would be appreciated.

So long,

Thomas.

Edited by thomas81
Link to comment
Share on other sites

Hi and thanks for the welcome :),

I tried your proposal but it didn't work for me. I think the INSTANCE:1 refers to the first instance of the CLASS:Button.

Since in the GUI I am trying to control buttons can be added and removed dynamically these instance ids of the buttons are not always the same and can change (tested and confirmed this just now).

Maybe I'll try getting all Buttons and compare the TEXT field myself with what I'm looking for.

Link to comment
Share on other sites

Autoit does click the controls with leading spaces...run this script

#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $hGUI, $Edit1, $Edit2, $Button1, $PID1, $PID2

#region ### START Koda GUI section ### Form=
$hGUI = GUICreate("Ping Test", 551, 443, -1, -1, BitOR($WS_SYSMENU, $WS_CAPTION, $WS_POPUP, $WS_POPUPWINDOW, $WS_BORDER, $WS_CLIPSIBLINGS), BitOR($WS_EX_TOPMOST, $WS_EX_TRANSPARENT))
$Edit1 = GUICtrlCreateEdit("", 8, 16, 529, 177)
GUICtrlSetLimit(-1, 9999999)
$Edit2 = GUICtrlCreateEdit("", 10, 201, 529, 177)
GUICtrlSetLimit(-1, 9999999)
$Button1 = GUICtrlCreateButton(" Press to Start", 224, 392, 105, 33, BitOR($BS_CENTER, $WS_GROUP), $WS_EX_STATICEDGE)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Exit()
        Case $Button1
            _Ping()
    EndSwitch
WEnd

Func _Ping()
    Local $gFile = EnvGet('SystemDrive') & "\Google.txt"
    Local $bFile = EnvGet('SystemDrive') & "\Bing.txt"
    _FileCreate($gFile) ;clear file
    _FileCreate($bFile) ;clear file
    GUICtrlSetData($Button1, " Ping in Progress")
    Local $PID1 = Run(@ComSpec & " /c ping -n 6 www.google.com > " & $gFile, EnvGet('SystemDrive') & "\", @SW_HIDE)
    Local $PID2 = Run(@ComSpec & " /c ping -n 6 www.bing.com > " & $bFile, EnvGet('SystemDrive') & "\", @SW_HIDE)
    While 1
        $gData = FileRead($gFile)
        $bData = FileRead($bFile)
        If GUICtrlRead($Edit1) <> $gData Then GUICtrlSetData($Edit1, $gData)
        If GUICtrlRead($Edit2) <> $bData Then GUICtrlSetData($Edit2, $bData)
        If Not ProcessExists($PID1) And Not ProcessExists($PID2) Then ExitLoop
    WEnd
    GUICtrlSetData($Button1, " Press to Start")
EndFunc   ;==>_Ping

Func _Exit()
    If ProcessExists($PID1) Then ProcessClose($PID1)
    If ProcessExists($PID2) Then ProcessClose($PID2)
    FileRecycle(EnvGet('SystemDrive') & "\Google.txt")
    FileRecycle(EnvGet('SystemDrive') & "\Bing.txt")
    Exit
EndFunc   ;==>_Exit

and test with this script

WinWaitActive('[TITLE:Ping Test; CLASS:AutoIt v3 GUI]')
ControlClick('[TITLE:Ping Test; CLASS:AutoIt v3 GUI]', '', '[CLASS:Button; TEXT: Press to Start]')

More than WinWait(), I woudl use WinWaitActive() directly before the ControlClick() if you are having issues. You can also use StringInStr() in conjunction with ControlGetText() if you are having serious issues differentiating between the controls

Edited by Varian
Link to comment
Share on other sites

Hi,

thanks for this reply. I tried your code and it also works for me when I use the GUI you provided.

When I change it to control my GUI it doesn't work any more.

WinWaitActive($windowname);
ControlClick($windowname, '', '[CLASS:Button; TEXT: About]');
MsgBox(0,"","done");

I get the msgbox but no button is pressed. The only difference I see is that my window is an SWT class. I'll attach what window info gave me.

post-62642-0-70000100-1296040329_thumb.p

Link to comment
Share on other sites

Try a ControlSend() with Enter first

WinWaitActive($Windowname)
ControlSend($Windowname, '', '[CLASS:Button; TEXT: About]', '{ENTER}')

If that does not work, test with this and report your results

WinWaitActive($Windowname)
While 1
    $oText = ControlGetText($Windowname, '', '[CLASS:Button; TEXT: About]')
    If Not $oText Then $oText = ControlGetText($Windowname, '', 'Button10')
    $Visible = ControlCommand($Windowname, '', '[CLASS:Button; TEXT:' & $oText & ']', 'IsVisible', '')
    If StringRegExp($oText, ' About', 0) And $Visible Then
        Do
            $Return = ControlClick($Windowname, '', '[CLASS:Button; TEXT:' & $oText & ']')
        Until $Return
        ExitLoop MsgBox(262208, 'Return Check', 'Text of button (space replaced with "#") = ' & StringReplace($oText, ' ', '#'))
    ElseIf Not $Visible Then
        ExitLoop MsgBox(262208, 'Error', 'Cannot Find Control')
    EndIf
WEnd
Edited by Varian
Link to comment
Share on other sites

Hi,

the first script did not work, but the second one now explained why:

the TEXT of the Button I wanted to press was " About" .. two whitespaces. Now that I use this string it works, I'm sorry for causing that trouble :) but now I'll go ask the GUI developer some nasty questions.

Thanks a lot to all of the nice people answering newbie questions so patiently.

Link to comment
Share on other sites

Hi,

the first script did not work, but the second one now explained why:

the TEXT of the Button I wanted to press was " About" .. two whitespaces. Now that I use this string it works, I'm sorry for causing that trouble :) but now I'll go ask the GUI developer some nasty questions.

Thanks a lot to all of the nice people answering newbie questions so patiently.

I was thinking that either there were actually more spaces or the "spaces" were some foreign (not typical) characters. Glad to have help a friend in need.

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...