View Full Version : [vb]Need Help with with web form completer script

Bry Man
01-28-2005, 02:49 AM

Sorry for the kinda cruddy title but ill explain it better. I have been researching how to fill out a web based form using vb for some time now so I could make a progam that logs into a web based account and runs some errands and then signs out, after a long time on google I finally found a site that had exactly what I was looking for so I took it and am trying to adapt it to my needs. Here is the script

Const User = "SomeUser"
Const Pass = "SomePass"

Private Sub Command1_Click()
wb1.Navigate2 "http://us.mcafee.com/root/login.asp"
Do: DoEvents: Loop Until wb1.ReadyState = READYSTATE_COMPLETE
wb1.Document.Forms("LoginForm").elements("UserID").Value = User
wb1.Document.Forms("LoginForm").elements("Password").Value = Pass
End Sub

And it corresponds to this web based form

<form action="Login.asp" method="post" id="LoginForm" name="LoginForm">
<div id="contentMain">
<input type="hidden" id="SUBMITTED" name="SUBMITTED" value="1" />
<input type="text" id="UserID" name="UserID" value="" style="width:175px" />
<input type="password" id="Password" name="Password" value="" style="width:175px" />
<input type="checkbox" id="RememberMe" name="RememberMe" />
<input type="submit" id="submit1" name="submit1" value=" Log In " style="font-size:11px" />

It works great for this form but the form im trying to use has alot of that info missing from it like the form name which is required as well as the submit buttons name. Here is the form im trying to have the program log into

<form action="login.php" method="post">

<input type="hidden" name="username">
<input type="hidden" name="pword">
<table align="center" class="small" style="padding: 0px 0px 5px 0px;" width="130">
<td align="center"><font color="black">Username:</font></td>
<td align="center"><input class="login_input" type="text" name="usrname" value=""></td>

<td align="center"><font color="black">Email:</font></td>
<td align="center"><input class="login_input" type="text" name="uemail" value=""></td>
<td align="center"><font color="black">Password:</font></td>

<td align="center"><input class="login_input" type="password" name="psword"></td>
<td align="center" style="padding-top: 5px;"><input class="login_input" type="submit" value="Login" style="width: 50px;"></td>

I am lost as to what I should do to make it log into the above form, if you could help me I would apprecate your time :)

*the vb code requires an instance of the webbrowser conrtol named wb1 to work*

01-28-2005, 05:25 AM
I'd think if the position of the elements is static, to use the collection index: forms(n).elements(n), or loop to compare DHTML or DOM properties, if not.

Bry Man
01-28-2005, 05:34 AM
Eh? Im fairly new to vb and ive spent alot of time just figuring out what to search for. Tell me a a bit more about what you mean cuz im a noob :D

01-28-2005, 05:43 AM
Try replacing n with a number which corresponds w/ the order in which the elements appear in the collections, such as forms(0).elements(5), or whatever...

Bry Man
01-28-2005, 06:51 AM
So what you mean is if I had 5 forms in a web page and each one had 3 elements ,which I assume makes a collection then, if I wanted to fill the 3rd form, 2nd element assuming the forms went in order 1 2 3... then I would have it be forms(3).elements(8)? correct me if im wrong, also what all is considered to be an 'element'?

01-28-2005, 07:08 AM
forms is a collection object which is property of document, and elements is a collection object which is a property of a given forms collection, so each form element in a document is part of the forms collection, and can be accessed by name or zero based index, as can each form element of a form, so when you have document.forms(0).elements(0), you are accessing the properties which return specific objects from each collection as such... in this case, the first.

Bry Man
01-28-2005, 07:12 AM
so what I post there was only off by one so itd be forms(2).elements(7), im sorry but im a little bit confused, im just learning this. so a form is considered a collection and the tags inside it like the input tages are considered elements? So like in the form I want to fill out there is 6 elements

01-28-2005, 07:50 AM
Right, you're working with two collections: forms and elements.
To get the elements collection, you'd first retrieve a specific form object.
If this is the first form in the document, it would be document.forms(0), or if the second, document.forms(1), etc.
Each form.elements element would be accessed in the same manner; for instance, if the button is the fifth, it would be document.forms(n).elements(4)

Also, just as wb1 represents an object, a reference to the collection may be stored, such as foo = wb1.document.forms(n).elements, and used to set properties from there on, as in foo(0).value = blah

Bry Man
01-28-2005, 04:03 PM
Alright, im starting to get it I think, ill play around with it a little today and If I cant get it working ill come back here.

Bry Man
01-28-2005, 04:22 PM
Awsome it works, thank you so much for your help with my questions :) . Now to go off and make my program, im also really gald firefox gets all the form info from the source for me so I dont have to go thru it myself, Go FireFox!

Bry Man
01-28-2005, 11:47 PM
I just hit another snag, what can be done if both the number of forms and elements changes so that a certain form/element may be the first form on one occasion and form number 5 on another, also the forms are unnamed but the Elements are like the following element as an example.

<input type="text" name="buy_weapon[11]" value="0" size="3">

This is a named element from an unnamed form that changes position in the page.

01-28-2005, 11:54 PM
Try: document.getElementsByName("buy_weapon[11]").item(0)

Bry Man
01-29-2005, 02:08 AM
Wow, how do you know this stuff lol, im just starting out with about 2 months of vb skills built up from school but I like a challenge so I thought Id do this becuase ive seen it done by others, thank you for all of your help I dunno what I woulda done without you because asking here was my last resort after a week of googling failed me.

01-29-2005, 02:25 AM
Memory's just a side-effect of repetition, er something... :) BTW, if you plan on experimenting often, then grabbing a local copy of the Platform SDK (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sdkintro/sdkintro/devdoc_platform_software_development_kit_start_page.asp) may save you a lot of time.