Sign in to follow this  
Followers 0
SentryP

Service-now.com login

19 posts in this topic

Can anyone tell me why this script isn't working?

I'm very new to autoit.

Any help would be great. Thank you

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <IE.au3>


Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $filemenu
    Local $exititem, $helpmenu, $aboutitem, $okbutton, $cancelbutton
    Local $PASSWORD, $ES_PASSWORD, $username, $oIE, $o_login, $o_password
    Local $msg, $file
    #forceref $separator1
    
    $ES_PASSWORD = 0x0020

    GUICreate("Service Now", 550, 550)

    $filemenu = GUICtrlCreateMenu("File")
    $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("Help")
    $aboutitem = GUICtrlCreateMenuItem("About", $helpmenu)
    
    
    GUICtrlCreateLabel("User Name: ", 10, 35)
    $username = GUICtrlCreateInput("admin", 70, 35, 100, 20)
    
    
    GUICtrlCreateLabel("Password: ", 10, 60)
    $password = GUICtrlCreateInput("admin", 70, 60, 100, 20, $ES_PASSWORD) 
    
    
    
    
    $okbutton = GUICtrlCreateButton("OK", 50, 130, 70, 20)

    $cancelbutton = GUICtrlCreateButton("Cancel", 180, 130, 70, 20)

    GUISetState()

    While 1
        $msg = GUIGetMsg()


        Select
            Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton
                ExitLoop

            Case $msg = $exititem
                ExitLoop

            Case $msg = $okbutton
                $oIE = _IECreate ("https://demo.service-now.com")
                _IELoadWait ($oIE)
                ; get pointers to the login form and username, password and signin fields
                $o_login = _IEGetObjByName ($oIE, "user_name")
                $o_password = _IEGetObjByName ($oIE, "user_password")
                ; Set field values and submit the form
                _IEFormElementSetValue ($o_login, GUICtrlRead($username))
                _IEFormElementSetValue ($o_password, GUICtrlRead($password))

            Case $msg = $aboutitem
                MsgBox(0, "About", "Service Now")
        EndSelect
    WEnd

    GUIDelete()

    Exit
EndFunc   ;==>_Main

Share this post


Link to post
Share on other sites



you must find the form first !


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

you must find the form first !

Ok I got the form name. It's "loginPage"

Can you please point me in the right direction? AKA, where should I look in the help file for code examples.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

_IEFormGetObjByName() is the function you need.

So after...

$oIE = _IECreate ("https://demo.service-now.com")
_IELoadWait ($oIE)

$myForm = _IEFormGetObjByName($oIE, "loginPage")

Then for your _IEGetObjectByName() functions, use $myForm instead of $oIE.

Edited by MrMitchell

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

yes but it doesn't work...

$oForms = _IEFormGetCollection ($oIE)
                $iNumForms = @extended
                ConsoleWrite ( "Forms Info There are " & $iNumForms & " forms on this page" & @Crlf )
                For $oForm in $oForms
                    ConsoleWrite ( "Form Info : " & $oForm.name & ' ' & $oForm.id & @Crlf )
                Next
                
                $oForm = _IEFormGetObjByName ($oIE, "loginPage")
                
                $o_login = _IEFormElementGetObjByName ($oForm, "user_name")
                _IEFormElementSetValue ( $o_login, GUICtrlRead($username))
                
                $o_password = _IEFormElementGetObjByName ($oForm, "user_password")
                _IEFormElementSetValue ($o_password, GUICtrlRead($password))

the form name get by IEFormGetCollection are not the same that i found! "loginPage"

Forms Info There are 2 forms on this page

Form name : 0, Form Id : textsearch

Form name : sys_personalize, Form Id : 0

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Maybe try frames...?

Local $oFrame ;Created with all the other variables
;....

$oIE = _IECreate ("https://demo.service-now.com")
_IELoadWait ($oIE)
$oFrame = _IEFrameGetCollection($oIE, 0)    ;Index 0 of the frame we need to work with

$o_login = _IEGetObjByName ($oFrame, "user_name")
$o_password = _IEGetObjByName ($oFrame, "user_password")
Edited by MrMitchell

Share this post


Link to post
Share on other sites

the form name get by IEFormGetCollection are not the same that i found! "loginPage"

Forms Info There are 2 forms on this page

Form name : 0, Form Id : textsearch

Form name : sys_personalize, Form Id : 0

I guess there are several frames and we're just working with the wrong one. The code I posted worked for me (using index 0 of frames collection)

Share this post


Link to post
Share on other sites

I guess there are several frames and we're just working with the wrong one. The code I posted worked for me (using index 0 of frames collection)

Well done ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Or heck use both if you want to be extra extra sure you're on the right form lol... not sure if it will actually work though.

$oIE = _IECreate ("https://demo.service-now.com")
_IELoadWait ($oIE)
                
$oFrame = _IEFrameGetCollection($oIE, 0)
$oForm = _IEFormGetObjByName($oFrame, "loginPage")
; get pointers to the login form and username, password and signin fields
$o_login = _IEGetObjByName ($oForm, "user_name")
$o_password = _IEGetObjByName ($oForm, "user_password")

Share this post


Link to post
Share on other sites

Well done ! Posted Image

Thanks ;)

To OP: Check out DebugBar, it's helpful when workin with IE.

Share this post


Link to post
Share on other sites

Thanks guys for the fast work..

I'm learning alot from the discussion!

Here is my next qwestion.

Once I have the form loaded, how do I determin the number of elements in the form?

I'm trying to use this code to "click" the Login button.

$oForm = _IEFormGetCollection($oFrame, 0); Retrieves the First form object in the page

$oSubmitButton = _IEFormElementGetCollection($oForm, 3); 5 because index starts at 0

_IEAction($oSubmitButton, "click"); Clicks on the button

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Well you've got on the form already so all you need to do now is find the name of the button and simulate a click on it. You shouldn't need to use _IEFormElementGetCollection because the button has a name. Just use "_IEFormElementGetObjByName"...

$bLogin = _IEFormElementGetObjByName($oForm, "not_important")
_IEAction($bLogin, "click")

If you view the source of the proper frame, search for "login" you get this line about a button creation...

<button id="sysverb_login" onclick="loginPage.sys_action.value=this.id;" class="login" type="submit" name="not_important">Login</button>

That line assigns the name "not_important" to the button so that's how I built the _IEFormElementGetObjByName() function.

Add: You also might just simply be able to use _IEFormSubmit() and give it the form's name. It appears that the button is the actual submit button; the [at least one] difference between the two methods is that the former with simulate a click and do everything else clicking the button may do (such as validate fields), whereas the later just submits the form. Sorry I can't explain it better

Edited by MrMitchell

Share this post


Link to post
Share on other sites

Well you've got on the form already so all you need to do now is find the name of the button and simulate a click on it. You shouldn't need to use _IEFormElementGetCollection because the button has a name. Just use "_IEFormElementGetObjByName"...

$bLogin = _IEFormElementGetObjByName($oForm, "not_important")
_IEAction($bLogin, "click")

If you view the source of the proper frame, search for "login" you get this line about a button creation...

<button id="sysverb_login" onclick="loginPage.sys_action.value=this.id;" class="login" type="submit" name="not_important">Login</button>

That line assigns the name "not_important" to the button so that's how I built the _IEFormElementGetObjByName() function.

Sorry I sould explain... On our production copy of service now the button is unnamed. I just using their demo site so I can give you guys access to see stuff.

Share this post


Link to post
Share on other sites

I'm really Thankful for all the help I have gotten.

If you guys are in MN/Twin Cities, Let me know and I will buy you a beer some time!

Share this post


Link to post
Share on other sites

Sorry I sould explain... On our production copy of service now the button is unnamed. I just using their demo site so I can give you guys access to see stuff.

Hmmm in that case have you tried just using _IEFormSubmit? Otherwise, use the collection function that you are currently using and run though all the buttons to find the correct name of the button. Or perhaps use DebugBar to get the name of it?

Share this post


Link to post
Share on other sites

Hmmm in that case have you tried just using _IEFormSubmit? Otherwise, use the collection function that you are currently using and run though all the buttons to find the correct name of the button. Or perhaps use DebugBar to get the name of it?

This is the part I don't understand how to get a list of form collections with their index number so I can see where the button is.

There is some java stuff on the onclick event on our prod website.

For many HTML forms it is not sufficient to use _IEFormSubmit() because there is often custom Javascript tied to an onclick event for its Submit button. In these cases you'll need to simulate a click of the submit button instead of using _IEFormSubmit(). See the example for the "click" action of _IEAction().

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Ok insert this code after you've attached $oForm:

make sure to declare variables first: $oFormElements, $index

$oFormElements = _IEFormElementGetCollection($oForm, -1)
$index = 0
For $each In $oFormElements
    ConsoleWrite("Index: " & $index & ", Name: " & $each.name & ", Type: " & $each.type & @CRLF)
    $index += 1
Next

The result output, in my case was:

Index: 0, Name: user_name, Type: text

Index: 1, Name: user_password, Type: password

Index: 2, Name: ni.nolog.user_password, Type: hidden

Index: 3, Name: ni.noecho.user_name, Type: hidden

Index: 4, Name: ni.noecho.user_password, Type: hidden

Index: 5, Name: remember_me, Type: checkbox

Index: 6, Name: screensize, Type: hidden

Index: 7, Name: sys_action, Type: hidden

Index: 8, Name: not_important, Type: submit

So in the above case, the name of my submit button is "not_important", index 8.

Edited by MrMitchell

Share this post


Link to post
Share on other sites

Ok insert this code after you've attached $oForm:

make sure to declare variables first: $oFormElements, $index

$oFormElements = _IEFormElementGetCollection($oForm, -1)
$index = 0
For $each In $oFormElements
    ConsoleWrite("Index: " & $index & ", Name: " & $each.name & ", Type: " & $each.type & @CRLF)
    $index += 1
Next

The result output, in my case was:

Index: 0, Name: user_name, Type: text

Index: 1, Name: user_password, Type: password

Index: 2, Name: ni.nolog.user_password, Type: hidden

Index: 3, Name: ni.noecho.user_name, Type: hidden

Index: 4, Name: ni.noecho.user_password, Type: hidden

Index: 5, Name: remember_me, Type: checkbox

Index: 6, Name: screensize, Type: hidden

Index: 7, Name: sys_action, Type: hidden

Index: 8, Name: not_important, Type: submit

So in the above case, the name of my submit button is "not_important", index 8.

Thank you, Thank you, Thank you

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

like this :

$oFormElements = _IEFormElementGetCollection ( $oForm, -1 )
                $index = 0
                For $each In $oFormElements
                    ConsoleWrite ( "Index: " & $index & ", Name: " & $each.name & ", Type: " & $each.type & ", Id: " & $each.Id & @CRLF )
                    $index += 1
                    If $each.name = 'not_important' Then 
                        $_Obj =_IEGetObjById ( $each, $each.Id )    
                        _IEAction ( $_Obj, 'click' )
                    EndIf
                Next

Or like this

$oFormElements = _IEFormElementGetCollection ( $oForm, -1 )
                $index = 0
                For $each In $oFormElements
                    ConsoleWrite ( "Index: " & $index & ", Name: " & $each.name & ", Type: " & $each.type & ", Id: " & $each.Id & @CRLF )
                    $index += 1
                    If StringInstr ( $each.Id , 'login' ) <> 0 Then 
                        $_Obj =_IEGetObjById ( $each, $each.Id )    
                        _IEAction ( $_Obj, 'click' )
                    EndIf
                Next

Login Button Id is sysverb_login

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

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