Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I've been fiddling with some code recently to automatically bring up an IE page, then use SendKeys to tab to the "username" field, enter the info, tab to the "password" field, enter the info, then finally hit ENTER. My code worked fine, but the IE page doesn't always load at the same speed, so I made some modifications based on a few code bits I found from various sources in an attempt to have the script wait until IE was completely loaded before proceeding with the SendKeys portion:
Option Explicit
Dim objShell, objIE
Set objShell = CreateObject("WScript.Shell")
Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate ("http://URLOnMyIntranet//?cid=6&c=12&cpc=dt2wJiVUfnQ2")
While objIE.Busy
wscript.sleep 2000
Wend
objShell.AppActivate "Internet Explorer"
objShell.SendKeys "{TAB}"
objShell.SendKeys "{TAB}"
objShell.SendKeys "username"
objShell.SendKeys "{TAB}"
objShell.SendKeys "password"
objShell.SendKeys "{ENTER}"
set objIE=Nothing
Set objShell=Nothing
WScript.QuitThe problem is that the script seems to do absolutely nothing. Where might I have gone wrong here? Thanks!

Well, for one, you didn't set objIE.Visible = True.
Also, you're using SendKeys when InternetExplorer.Application provides its own text input.
Option Explicit
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate "http://URLOnMyIntranet//?cid=6&c=12&cpc=dt2wJiVUfnQ2"
Do While .Busy
WScript.Sleep 100
Loop
.Document.getElementByID("usernameInput").Value = username
.Document.getElementByID("passwordInput").Value = password
'Note: You could just get the form and submit it, but
'you'll miss out on any special JavaScript associated
'with the Submit button.
.Document.getElementByID("SubmitButton").Click
End With

I really need to either find a decent VBS guide or class; so far everything I know has been based off of picking apart other scripts I've run across (which is fine for learning how to write batch files, but VBS seems to be a different animal entirely).
I'm gathering that "usernameInput" is the defined name of the field, and that "username" is the actual username to be entered. When I ran it, I got an error stating that the variable "username" (after I changed it to the actual username, of course) was undefined. I may have this backwards; I'm just not familiar with some of these commands. Here's the relevant code for the web page:
<tr>
<td align=right class="defaultText">Username : </td>
<td><input type=text name="username" value=""></td>
</tr>
<tr>
<td align=right class="defaultText">Password : </td>
<td><input type=password name="password" value=""></td>
</tr>This may be something simple, but I haven't been able to figure out why I'm getting this message yet. Thanks for your help!

That form uses the name property, not id. You'll need to adjust the script accordingly
Option Explicit
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate "http://URLOnMyIntranet//?cid=6&c=12&cpc=dt2wJiVUfnQ2"
Do While .Busy
WScript.Sleep 100
Loop
.Document.getElementsByName("username").Item(0).Value = username
.Document.getElementsByName.("password").Item(0).Value = password
'Note: You could just get the form and submit it, but
'you'll miss out on any special JavaScript associated
'with the Submit button.
.Document.getElementsByName("Submit").Item(0).Click
End With
VBScript is easy; the problem is everything VBS can interface with.

The powers that be have just saddled me with a new project that takes precedence over this one, so I'm going to have to shelve this for now, but I've saved everything, and will definitely try it out when I get the chance. Thanks for your help!!!

Okay, I got the web browser bit working; I was wondering how it might be adapted to other third-party applications (run a program, wait for the login dialog window to appear, enter the info, then send a carriage return or click a button.
Thanks!

Unless the application has a script-able ActiveX interface, you'll need to use SendKeys, which you already know how to use.
Quick note, though. You can use "~" as a shortcut for "{ENTER}".

![]() |
scripting /win xp /noobis...
|
Scripting opinion
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |