Sign in to follow this  
Followers 0
jchensor

ControlClick and Control IDs

11 posts in this topic

I'm new to AutoIt and I will issue advanced apologies, as I suspect this question has been asked many times. I did do a search through the Forums, but never found a solution that worked for my requirements.

I have been trying very hard to get AutoIt to click on a button by Control ID. Throughout my various tests, I've managed to get it to work using Button Text and ClassNameNN... but for the situations I am using this script, neither of those will work for me. I really need to use the Control ID. But no matter what I try, I can't get it to work. And the script is just running a simple ControlClick call:

; return 1 if control is found, otherwise 0
Func Click($window,$control)
    Return ControlClick($window,"",$control)
EndFunc

$window and $control are set by command-line parameters. So when I pass in the Window Title and, say, "OK" for the $control (the button's text), it works. If I use "Button1" for $control (the button's ClassNameNN), it works. But when I use "5303" (the button's Control ID), it does NOT work.

I've seen in various threads, now, people claiming they have this problem, but the solution presented by other posters seems to always be "Try using buttontext or ClassNameNN instead." But for what I'm trying to accomplish, I need it to work on Button IDs. Any ideas on what could possibly be going wrong?

Thanks a bunch.

- James

Share this post


Link to post
Share on other sites

Just to elaborate on Larry's post and to confess:

ControlClick ( "title", "text", controlID [, button] [, clicks]] )

ControlClick ( "title", "text", "ClassNameNN" [, button] [, clicks]] )

That little tidbit escaped me for sometime... hence my custom title of MSP.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Thanks for the quick responses!

I did try with and without quotes, and both times it failed. I'm currently running a quick test instead of trying it on my main application that I'm testing. What I'm doing is opening the Run Dialog and typing gibberish into the Run Dialog. What happens is that an error dialog comes up telling me that the gibberish I type in isn't a recognized app, and there is an "OK" button there.

So now I'm running my script looking for the "OK" button, which has a Control ID of 2. When I pass "sadsa" into the script (that's the gibberish I consistently type, and it becomes the title of that error dialog) as the Window Title and "OK" as the button text, it works. When I pass in "Button1", it also works because it is the ClassNameNN. But, according to Au3Info.exe, the Control ID is 2. When I pass in 2 or "2", neither works.

I continue to fiddle with it, however. If I find anything that finally works, I'll let everyone know. Thanks again for the quick replies!

- James

Share this post


Link to post
Share on other sites

To help out a bit, here's my entire script that I'm running for this quick test:

; Wait for the window to become active (title, text, timeout)
while(true)
    If(1==WinWait("sadsa","",10)) Then 
        WinActivate("sadsa")
        Local $window = WinGetTitle("")
        If($window=="") Then
            ContinueLoop
        Else
            Click($window, $CmdLine[1])
        EndIf
    EndIf
WEnd

Func Click($window,$control)
    Local $return = ControlClick($window,"",$control)
    If(0==$return)Then
        MsgBox(1, "Return Value", "Fail!")
    EndIf
    If(1==$return)Then
        MsgBox(1, "Return Value", "Success!")
    EndIf
EndFunc

And here's the button's data:

>>>>>>>>>>> Control Under Mouse <<<<<<<<<<<

Size: X: 339 Y: 60 W: 75 H: 23

Control ID: 2

ClassNameNN: Button1

Text: OK

Style: 0x50030001

ExStyle: 0x00000004

When I pass in "OK" and "Button1", I get success. When I pass in "2" or 2, I get failure.

Thanks again.

- James

Share this post


Link to post
Share on other sites

Please run this uncompiled from within the SciTE editor:

WinWait("sadsa")
WinActivate("sadsa")
WinWaitActive("sadsa")
$return = ControlClick("sadsa", "", 2)
If Not $return Then MsgBox(0, $return, "Fail!")
If $return Then MsgBox(0, $return, "Success!")
;;;
WinWait("sadsa")
WinActivate("sadsa")
WinWaitActive("sadsa")
$return = ControlClick("sadsa", "", "Button1")
If Not $return Then MsgBox(0, $return, "Fail!")
If $return Then MsgBox(0, $return, "Success!")
...it will probably yield the same results, but it takes a few things out of the mix.

-MSP-


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

herewasplato:

Interestingly enough, it actually did work! The only real difference I see is the WinWaitActivate call. So do you think my original script was trying to press the button before the window actually becomes fully active? 'Cause with my original script, I can keep clicking the "OK" button on my "Fail!" dialog, and it would continually fail. The error message dialog is definitely active.

Have any idea what the cause might be?

Thanks a bunch!!

- James

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

After doing some further research, I've discovered it has something to do with the way command-line parameters are processed. In my original script, if I hard-code "2" in one of two places, the script works fine:

1) When I call my "Click" function, if I pass in 2 instead of "$CmdLine[1]", it works.

2) When I actually call ControlClick, if I replace "$control" with 2, it works.

So thanks, so far, herewasplato. You're definitely leading me down the right track. Now I just need to figure out how to pass in the Control ID via command-line and still have it work. If I get it working, I'll post here right away.

Edit to add: I noticed that if I hardcode "2" instead of just 2 (without quotes), the edits I used above don't work anymore. So apparently, ControlClick only recognizes them as Control IDs if they are actual integer values (or whatever the AutoIt equivalent is). Command-line parameters must be treated as strings no matter how you pass them in, causing my script fail. I just need to figure out how to convert the string of "2" and turn it into an integer value.

- James

Edited by jchensor

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Okay, I got it to work. Thanks a bajillion, herewasplato. Even if you indeirectly helped me solve the problem, I don't think I would have figured out the problem without your post! Many many thanks.

For those who have run into a similar problem as mine and have found this thread, here is the final script I came up with:

; Wait for the window to become active (title, text, timeout)
while(true)
    If(1==WinWait($CmdLine[1],"",10)) Then 
        WinActivate($CmdLine[1])
        Local $window = WinGetTitle("")
        If($window=="") Then
            ContinueLoop
        Else
            If(1==StringIsInt($CmdLine[2]))Then
            ;Do String conversion to Int.
                Click($window, Number($CmdLine[2]))
            Else
                Click($window, $CmdLine[2])
            EndIf
        EndIf
    EndIf
WEnd

Func Click($window,$control)
    Local $return = ControlClick($window,"",$control)
    If(0==$return)Then
        MsgBox(1, "Return Value", "Fail!")
    EndIf
    If(1==$return)Then
        MsgBox(1, "Return Value", "Success!")
    EndIf
EndFunc

Thanks again!

- James

Edited by jchensor

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

It doesn't work because a command line is a string. You have to convert the command line from a string to an integer to get it to work:

; Wait for the window to become active (title, text, timeout)
while(true)
    If(1==WinWait("sadsa","",10)) Then
        WinActivate("sadsa")
        Local $window = WinGetTitle("")
        If($window=="") Then
            ContinueLoop
        Else
            Click($window, $CmdLine[1])
        EndIf
    EndIf
WEnd

Func Click($window,$control)
    Local $return = ControlClick($window,"",Int($control));it now converts the string command line into an actual integer
    If(0==$return)Then
        MsgBox(1, "Return Value", "Fail!")
    EndIf
    If(1==$return)Then
        MsgBox(1, "Return Value", "Success!")
    EndIf
EndFunc

-The Kandie Man ;-)

EDIT:

Looks like you already solved the problem. I will have to refresh the page more often. ;-)

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

...Many many thanks...

You are welcome.

...enjoy AutoIt...


[size="1"][font="Arial"].[u].[/u][/font][/size]

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
Sign in to follow this  
Followers 0