Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if form exists? 1

Status
Not open for further replies.

cv433

Technical User
Sep 16, 2009
3
DK
Hello

I am trying to customize a vbscript that fill web forms like this one:


Is it possible to add some code that check if the field exists before filling with data. Ex. if the field fld_userName do not exists the script should just open the page in IE an do nothing more, and if it does exists it should fill the forms?
 
Explicitly shown the relevant "form" anticipated in the page you want to fill. You wouldn't ask us to digest what said over there and what solved and what unsolved. Post a self-contained problem.
 
Okay that seems reasonable.

This code fill username and password forms in a webpage:

Set IE = CreateObject("InternetExplorer.Application")
set WshShell = WScript.CreateObject("WScript.Shell")
IE.Navigate "IE.Visible = True
Wscript.Sleep 6000
IE.Document.All.Item("fld_userName").Value = "adley"
IE.Document.All.Item("fld_password").Value = "password"
WshShell.AppActivate "IE"
WshShell.SendKeys "{ENTER}"

This works great, but if the script is executed when the user is already logged in, an error message is shown beacause the field fld_userName and fld_password does not exists when logged in, so the script cannot fill the.

What i want to do is to check for a string that indicates if the user is already logged in, before trying to insert anything.

In Linux i would do someting like this:

check_if_logged_in=$(wget -O - | grep -o "Welcome you are logged in");

if &check_if_logged_in != 0 then
fill forms

else
exit script

How can this be accomplished with vbscript?

else
 
[1] Check them like this.
[tt]
if not IE.Document.All.Item("fld_userName") is nothing then
IE.Document.All.Item("fld_userName").Value = "adley"
IE.Document.All.Item("fld_password").Value = "password"
'etc etc
else
set IE=nothing
wscript.quit
end if
[/tt]
[2] Using wshshell sendkeys method means always a inferior method of script short of better approach. For the handling of html, you should use much better method to handle the automation.

[2.1] Determine the name (attribute) of the form. In case you really don't know yet, suppose it is the first or the only form (hereinafter, reflecting in the use of forms(0)). The whole functional part would appear like this.
[tt]
With IE.document
if not .forms(0) is nothing then
if not .forms(0).elements("fld_userName") is nothing
.forms(0).elements("fld_userName")="adley"
.forms(0).elements("fld_password")="password"
.forms(0).submit
else
'this part seems repetitive with the outer if
'you can make improvement on the structure a bit
set IE=nothing
wscrpt.quit
end if
else
set IE=nothing
wscript.quit
end if
end with
[/tt]
[2.2] You won't find sendkeys etc in the [2.1]. You can take wshshell part all out.

[2.3] If you know the name of the form, say "formname", you can replace forms(0) by forms("formname"). The rest is the same.

 
Thank you very much. With the information you provided i was able to get the script working like i wanted to.

I would like to take a closer look at 2.1 at a later time to optimize the script, because as you has said wshshell sendkeys is not the right way.

Thanks again
 
I had a typo there. The line should obviously read like this.
[tt] if not .forms(0).elements("fld_userName") is nothing [red]then[/red][/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top