Jump to content

Trying to auto login a website and accepting the second page


Recommended Posts

Hey guys,

I'm having some trouble with a script, wondering if someone can help me with it.

I'm trying to auto-login a website, but when another user is logged in already it presents a second screen you have to accept before you actually login.

When there is no other user logged in, the second page doesn't come up, and you're logged in emediately, so a check would be nice, but not needed I think.

The code I have so far is this :

#include <IE.au3>
#include <misc.au3>

$oIE = _IECreate ("http://idp.sdu.nl/idp/Curtains/chemiekaarten/inlogPagina.jsp?providerid=Y2hlbWlla2FhcnRlbg%3D%3D&logonstate=116625211")

$o_form = _IEFormGetCollection ($oIE, 0) 
$o_login = _IEFormElementGetCollection ($o_form, 0) 
$o_password = _IEFormElementGetCollection ($o_form, 1) 

$username = "user"
$password = "pass"

_IEFormElementSetValue ($o_login, $username)
_IEFormElementSetValue ($o_password, $password)

Sleep(2000)
_IEFormSubmit ($o_form)
_IELoadWait ($oIE)

from there (this part works) it takes me to the attached HTML document and I need to "post" that form too to login. I can't get that part to work.

Does anyone know how to do this ?

preferably after this second page is accepted (so I'm logged in as a user) I'd like the website to open a different page : http://chemiekaarten.sdu.nl/chkonline/

Any help would be appreciated!

JJ

page2.htm

Link to comment
Share on other sites

So... what does the script look like that handles the second page (after the _IELoadWait)? You said "I can't get that part to work", but you didn't post that part.

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

well, to be honoust, I have no clue what to use for the second part, so I've been looking at snippets and trying those parts in the code, but nothing of it worked, I basically only need the "submit" button pressed on the second page, just like it's succesfully pressed on the first page (the code in the HTML looks exactly the same).

JJ

Link to comment
Share on other sites

I was thinking this might do the trick for the button on the second page before you actually login, but it doesn't work :mellow:

#include <IE.au3>
#include <misc.au3>

$oIE = _IECreate ("http://idp.sdu.nl/idp/Curtains/chemiekaarten/inlogPagina.jsp?providerid=Y2hlbWlla2FhcnRlbg%3D%3D&logonstate=116625211")

$o_form = _IEFormGetCollection ($oIE, 0) 
$o_login = _IEFormElementGetCollection ($o_form, 0) 
$o_password = _IEFormElementGetCollection ($o_form, 1) 

$username = "user"
$password = "pass"

_IEFormElementSetValue ($o_login, $username)
_IEFormElementSetValue ($o_password, $password)

Sleep(2000)
_IEFormSubmit ($o_form)
_IELoadWait ($oIE)

Sleep (2000)

$next = _IEGetObjByName($oIE, "uitgebreidzoekenknop")
_IEAction($next, "click")
Link to comment
Share on other sites

Why create a whole new instance of IE? After you submit the first page and wait for that to complete, are you already at the next page you need to work with? If so, just carry on with the $oIE you already have. Note that when you navigate to a new page, your $oIE is still valid, but everything from the DOM like $o_form is invalidated and has to be retrieved anew.

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Why create a whole new instance of IE? After you submit the first page and wait for that to complete, are you already at the next page you need to work with? If so, just carry on with the $oIE you already have. Note that when you navigate to a new page, your $oIE is still valid, but everything from the DOM like $o_form is invalidated and has to be retrieved anew.

:mellow:

Can you give me a code example of how that would look like? I'm lost since I'm thinking I'm not creating a whole new IE with the code I posted last, the first part of the script does take me to the second page (the page2.html I posted) where I need to click on the "login" button again. if you open the page2.html it'll become clear how the second page looks like.

How can I retrieve the second page form again and make that one "post"/"login" as well?

I have no clue how to stay in the same "IE" instance and work from there. please help.

Edited by barkeeper
Link to comment
Share on other sites

You do it the second time exactly as you did the first time:

#include <IE.au3>

; First page
$sURL = "http://www.google.com"
$oIE = _IECreate($sURL)
$oForm = _IEFormGetObjByName($oIE, "f")
$oInput = _IEFormElementGetObjByName($oForm, "q")
_IEFormElementSetValue($oInput, "AutoIt")
_IEFormSubmit($oForm)

Sleep(2000)

; Second page, $oIE is still the same, but $oForm and $oInput are now invalid
;   and must be retrieved again
$oForm = _IEFormGetObjByName($oIE, "gs")
$oInput = _IEFormElementGetObjByName($oForm, "q")
_IEFormElementSetValue($oInput, "IE.au3 site:www.autoitscript.com")
_IEFormSubmit($oForm)

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Doesn't appear to be working for some reason, could it be that the fact the URL changes to :

http://idp.sdu.nl/idp/Curtains/chemiekaarten/confirmLogin.jsp?providerid=Y2hlbWlla2FhcnRlbg%3D%3D&logonstate=117161011

Could be the problem ?

This is what I have at the moment :

#include <IE.au3>
#include <misc.au3>

$oIE = _IECreate ("http://idp.sdu.nl/idp/Curtains/chemiekaarten/inlogPagina.jsp?providerid=Y2hlbWlla2FhcnRlbg%3D%3D&logonstate=116625211")

$o_form = _IEFormGetCollection ($oIE, 0) 
$o_login = _IEFormElementGetCollection ($o_form, 0) 
$o_password = _IEFormElementGetCollection ($o_form, 1) 

$username = "user"
$password = "pass"

_IEFormElementSetValue ($o_login, $username)
_IEFormElementSetValue ($o_password, $password)

Sleep(2000)
_IEFormSubmit ($o_form)
_IELoadWait ($oIE)

Sleep (3000)

$oForm = _IEFormGetObjByName($oIE, 0)
$oInput = _IEFormElementGetObjByName($oForm, 1)
_IEFormSubmit($oForm)
Link to comment
Share on other sites

Doesn't appear to be working for some reason...

Determine the reason. Put _IEErrorHandlerRegister() near the top of your script and run it in SciTE so you can watch the output to the console. For example, on the second page the form might be inside a frame and you would see that _IEFormGetObjByName($oIE, 0) is failing.

I only put the Sleep() in there to give the user time to see what happened before it navigated away.

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hey Psaltyds,

I've tried that, but the script keeps on running eventhough the second page is already shown, and in the end I need to terminate the application.

>Process failed to respond; forcing abrupt termination...

>Exit code: 1 Time: 47.052

Link to comment
Share on other sites

Is it hanging on the _IELoadWait()? Some web pages never finish loading (as far as _IELoadWait() is concerned) because of some kind of Java stuff, animations, and whatnot is running.

Use a different method to detect sufficient completion of the second page, like attempting to get a reference to a particular element on the new page in a loop until it succeeds. You'll need to be more specific, because just form index 0, for example, will exist on the old page as well as the new one.

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

To work with automating web pages, you really have no choice but to climb the learning curve a little on the IE Document Object Model (DOM). That is, the design of the programming interface to an HTML document. For instance, it defines what a link is, that a link can have an ID property, and if it does you can retrieve it by using $oLink.id.

So what I was describing requires digging in to the DOM on the particular page you are interested in. Find something (an element) unique on the second page that isn't there on the first page. Once the first page has been dealt with and you are waiting for the second page, check for that unique element in a loop until it becomes available. Here's an example that waits for a form named "SecondPageForm" to appear:

#include <IE.au3>

_IEErrorHandlerRegister()

; ...the rest of the script

_IEFormSubmit($oForm, False) ; Submit first page form, don't wait

$iTimer = TimerInit() ; Timeout timer
While 1
    $oForm = _IEFormGetObjByName($oIE, "SecondPageForm") 
    If Not @error And IsObj($oForm) Then ExitLoop
    
    ; check 60sec timer
    If TimerDiff($iTimer) >= 60000 Then 
        MsgBox(16, "Error", "Second page did not load before timeout.")
        Exit
    EndIf
    Sleep(100)
WEnd

MsgBox(64, "Success", "Detected second page exists.")

The next problem is how to know what you're looking for. You can examine the source of the page by just View|Source in IE, or you can install DebugBar or some other DOM Inspector and use that to find a unique element on the page.

All this is to deal with one problem: A page that never finishes loading or signals "Done" because of scripts or apps it runs while open.

:mellow:

Edit: Should have included $f_wait = False on _IEFormSubmit().

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

To work with automating web pages, you really have no choice but to climb the learning curve a little on the IE Document Object Model (DOM). That is, the design of the programming interface to an HTML document. For instance, it defines what a link is, that a link can have an ID property, and if it does you can retrieve it by using $oLink.id.

So what I was describing requires digging in to the DOM on the particular page you are interested in. Find something (an element) unique on the second page that isn't there on the first page. Once the first page has been dealt with and you are waiting for the second page, check for that unique element in a loop until it becomes available. Here's an example that waits for a form named "SecondPageForm" to appear:

#include <IE.au3>

_IEErrorHandlerRegister()

; ...the rest of the script

_IEFormSubmit($oForm) ; Submit first page form

$iTimer = TimerInit() ; Timeout timer
While 1
    $oForm = _IEFormGetObjByName($oIE, "SecondPageForm") 
    If Not @error And IsObj($oForm) Then ExitLoop
    
    ; check 60sec timer
    If TimerDiff($iTimer) >= 60000 Then 
        MsgBox(16, "Error", "Second page did not load before timeout.")
        Exit
    EndIf
    Sleep(100)
WEnd

MsgBox(64, "Success", "Detected second page exists.")

The next problem is how to know what you're looking for. You can examine the source of the page by just View|Source in IE, or you can install DebugBar or some other DOM Inspector and use that to find a unique element on the page.

All this is to deal with one problem: A page that never finishes loading or signals "Done" because of scripts or apps it runs while open.

:mellow:

This is really turning out to be a pickle :(

The code on the HTML/Jsp page lists :

<form name="confirmationForm" id="confirmationForm" method="post" name="confirm" action="/idp/confirmLogin" target=_top>

should I use confirmationForm or confirm in :

$oForm = _IEFormGetObjByName($oIE, "SecondPageForm")

?

for now with both versions the page doesn't do anything, no message about the second page appears, not even an error in scite :lol:

Edited by barkeeper
Link to comment
Share on other sites

Hmm... I would have thought that two 'name' attributes in the same tag would invalidated it and throw errors about bad HTML.

Try _IEGetObjByID($oIE, "confirmationForm") instead of _IEFormGetObjByName().

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Is there some bug known with Scite ? when I leave the script running for like 5 minutes, all of a sudden the script continues, it seems to hang after _IEFormSubmit

Any thoughts ?

I made the $itimer 1000, this is what I got from scite (Finally!)

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\struisj\Desktop\ielogin.au3"

--> IE.au3 V2.4-0 Warning from function _IELoadWait, $_IEStatus_LoadWaitTimeout

>Exit code: 0 Time: 356.730

Edited by barkeeper
Link to comment
Share on other sites

Several functions like _IEFormSubmit() call _IELoadWait() internally by default, and under that in the help file:

$i_timeout = [optional] Period of time to wait before exiting function (default = 300000 ms aka 5 min)

I should have included the $f_wait = False parameter in the _IEFormSubmit() example I posted because we want to handle the waiting ourselves. Change it to:
; ...the rest of the script

_IEFormSubmit($oForm, False) ; Submit first page form, don't wait

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Finally got it to work! You're a star! I wouldn't EVER have gotten this myself.

Working code :

#include <IE.au3>
#include <misc.au3>

_IEErrorHandlerRegister() 

$oIE = _IECreate ("http://idp.sdu.nl/idp/Curtains/chemiekaarten/inlogPagina.jsp?providerid=Y2hlbWlla2FhcnRlbg%3D%3D&logonstate=116625211")

$o_form = _IEFormGetCollection ($oIE, 0) 
$o_login = _IEFormElementGetCollection ($o_form, 0) 
$o_password = _IEFormElementGetCollection ($o_form, 1) 

$username = "user"
$password = "pass"

_IEFormElementSetValue ($o_login, $username)
_IEFormElementSetValue ($o_password, $password)
_IEFormSubmit ($o_form, False)

$iTimer = TimerInit() ; Timeout timer

While 1
    $o_Form = _IEGetObjByID($oIE, "confirmationForm") 
    If Not @error And IsObj($o_Form) Then ExitLoop


    If TimerDiff($iTimer) >= 30000 Then 
        MsgBox(16, "Error", "You can specify a message here to show if the second page hasn't loaded yet after 30 seconds.")
        Exit
    EndIf
    Sleep(100)
WEnd

_IEFormSubmit ($o_form, False)
Link to comment
Share on other sites

  • 2 years later...

hey guys I like this code but it only work on certaint sites

how do I get this to work for my google login?

#include <IE.au3>

#include <misc.au3>

_IEErrorHandlerRegister()

$oIE = _IECreate ("https://accounts.google.com/")

$o_form = _IEFormGetCollection ($oIE, 0)

$o_login = _IEFormElementGetCollection ($o_form, 0)

$o_password = _IEFormElementGetCollection ($o_form, 1)

$username = "user"

$password = "pass"

_IEFormElementSetValue ($o_login, $username)

_IEFormElementSetValue ($o_password, $password)

_IEFormSubmit ($o_form, False)

$iTimer = TimerInit() ; Timeout timer

Edited by tax
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...