Sign in to follow this  
Followers 0
dvlkn

button & check box

7 posts in this topic

Hello

I am fairly new to autoIT and I am also a beginner in visual basic. I am trying to automate web surfing and I want the bot to login (given username and pass in text boxes) and perform whatever function the 'ticked' check boxes are associated to.

So here's my GUI:

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 384, 241, 249, 222)
$txtuser = GUICtrlCreateInput("", 120, 16, 153, 21)
$Label1 = GUICtrlCreateLabel("Username:", 24, 16, 55, 17)
$Label2 = GUICtrlCreateLabel("Password: ", 24, 48, 56, 17)
$txtpass = GUICtrlCreateInput("", 120, 48, 153, 21)
$Button1 = GUICtrlCreateButton("Start", 256, 96, 49, 33, 0)
$Group1 = GUICtrlCreateGroup("Group1", 32, 96, 105, 121)
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 48, 120, 73, 17)
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox2", 48, 152, 73, 17)
$Checkbox3 = GUICtrlCreateCheckbox("Checkbox3", 48, 184, 73, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Now I know that I have to add GUICtrlSetOnEvent($Button1, "buttonclick") to set the event 'buttonclick' so I placed it after

$Button1 = GUICtrlCreateButton("Start", 256, 96, 49, 33, 0)

And this is the associated function:

Func buttonclick()
    ShellExecute ("c:\program files\mozilla firefox\Firefox.exe", "http://www.url.com")
    WinWaitActive ("Mozilla Firefox")
    
    Sleep (3000)
    
MouseClick ("Left", 868, 211)
Sleep (500)
Send ("$txtuser")
Send ("{TAB}")
Send ("$txtpass")
Send ("{ENTER}")


EndFunc

When I run it, the GUI appears. But when I click the button, nothing happens? Where did I go wrong ?

Also what is the proper code to attach a function to a check box ?

Any help on this would be greatly appreciated :)

Share this post


Link to post
Share on other sites



You cannot mix OnEvent code with GUIGetMsg() code. You have to decide which one to use (I would recommend OnEvent mode).

Here's your code in OnEvent Mode and events for the button and one of the check boxes :)

#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode",1) ; New
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 384, 241, 249, 222)
$txtuser = GUICtrlCreateInput("", 120, 16, 153, 21)
$Label1 = GUICtrlCreateLabel("Username:", 24, 16, 55, 17)
$Label2 = GUICtrlCreateLabel("Password: ", 24, 48, 56, 17)
$txtpass = GUICtrlCreateInput("", 120, 48, 153, 21)
$Button1 = GUICtrlCreateButton("Start", 256, 96, 49, 33, 0)
GUICtrlSetOnEvent(-1,"Buttonclicked")
$Group1 = GUICtrlCreateGroup("Group1", 32, 96, 105, 121)
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 48, 120, 73, 17)
GUICtrlSetOnEvent(-1,"Check1Clicked")
$Checkbox2 = GUICtrlCreateCheckbox("Checkbox2", 48, 152, 73, 17)
$Checkbox3 = GUICtrlCreateCheckbox("Checkbox3", 48, 184, 73, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetOnEvent($GUI_EVENT_CLOSE,"close") ; New)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd


Func close () ; New
    Exit
EndFunc

Func Buttonclicked()
    MsgBox(0,"Info","You clicked the start button!")
EndFunc

Func Check1Clicked()
    If BitAND(GUICtrlRead($Checkbox1),$GUI_CHECKED)=$GUI_CHECKED Then
        MsgBox(0,"Info","Checkbox1 is now checked")
    Else
        MsgBox(0,"Info","Checkbox1 is now unchecked")
    EndIf
EndFunc

:(


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Thanks a lot :) now I can continue working on this. I'll report here if I get any further problems :(

Share this post


Link to post
Share on other sites

Umm a little problem: I cant get the script to enter the username and password in the blanks on the web page =/

I created two text boxes for the user to input his username and password. On clicking the button, it launches the browser correctly onto the desired page.

Then it clicks in the blanks on that page and this is where it goes wrong, it does not enter the username and password as typed previously in the text boxes. Where is my error ?

Func Buttonclicked()
    
    ShellExecute ("c:\program files\mozilla firefox\firefox.exe", "http://www.url.com")
    WinWaitActive ("Mozilla Firefox")
    
    Sleep (3000)
    
MouseClick ("Left", 868, 211)
Sleep (500)
Send ($txtuser)
Send ("{TAB}")
Send ($txtpass)
Send ("{ENTER}")



EndFunc

Share this post


Link to post
Share on other sites

Umm a little problem: I cant get the script to enter the username and password in the blanks on the web page =/

I created two text boxes for the user to input his username and password. On clicking the button, it launches the browser correctly onto the desired page.

Then it clicks in the blanks on that page and this is where it goes wrong, it does not enter the username and password as typed previously in the text boxes. Where is my error ?

Func Buttonclicked()
    
    ShellExecute ("c:\program files\mozilla firefox\firefox.exe", "http://www.url.com")
    WinWaitActive ("Mozilla Firefox")
    
    Sleep (3000)
    
MouseClick ("Left", 868, 211)
Sleep (500)
Send ($txtuser)
Send ("{TAB}")
Send ($txtpass)
Send ("{ENTER}")



EndFunc
Providing part of the script is not much a big help as I cannot locate your error or anything. Full script would be appreciated as I can help integrating _IE functions.

Share this post


Link to post
Share on other sites

You need to actually get the text from the edit Controls. not just pass in the control ID's. eg:

Sleep (500)
Send (GuiCtrlRead($txtuser))
Send ("{TAB}")
Send (GuiCtrlRead($txtpass))
Send ("{ENTER}")

Share this post


Link to post
Share on other sites

Thanks to you all, I managed to do what I wanted :)

A friend of mine told me about IE functions and said it could be more efficient. I understand that I need to use functions like: _IEGetObjById or IEGetObjByName. Only problem is that on the login page, the form does not have any name (nor could I find its ID :S) any help on this ?

The website is a web browser based game called Starkingdoms

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