Name: keridbey Date: March 25, 2008 at 12:00:26 Pacific Subject: VBS to SendKeys after app loads OS: WinXP CPU/Ram: 1.83 Ghz / 1 Gb Model/Manufacturer: Dell
Comment:
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.Quit
The 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:
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.
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE