Login to a Website using VBscript
Posted on July 8th, 2018
Here is a simple way to automate logging into a website using VBscript.
'Begin program by calling the Main function.Call Main'End programFunction Main'Create Internet Explorer Application ObjectSet IE = WScript.CreateObject("InternetExplorer.Application")'Set the created objects visibility to true'You will see Internet explorer open'If you do not want to see Internet Explorer open then set this to FalseIE.Visible = True'Navigate to the login pageIE.Navigate "http://www.somedomain.com/someloginpage.com";'Wait for page to finish loadingWait IE'Populate credentials and hit the submit buttonWith IE.Document'You will need to view the source of the login page you would like to'automate logging into. View the source to get the ID values for the username'input box and password input box..getElementByID("css-input-id-goes-here").value = "username".getElementByID("css-input-id-goes-here").value = "password"Call IE.Document.Forms(X).Submit()'Or Call Call IE.getElementByID("css-input-id-goes-here").Click()End IfEnd With'End IFEnd Function'Wait for the web page to load before continuingSub Wait(IE)DoWScript.Sleep 900Loop While IE.ReadyState < 4 And IE.BusyEnd Sub
First create a Sub called Wait that takes one parameter. This Wait Sub will wait for the page to load before continuing. Now, create a function called Main. This function will navigate to the login form and populate the username, password, and hit the submit button.
The Main function starts by navigating to the login page. In the script replace the "http://www.somedomain.com/somloginpage.com"; with your specified login page you would like to automate logging into. The function then calls the Wait Sub with the IE application object as the parameter. The Wait Sub waits for the page to fully load before the script continues to entering the page credentials.
To automate entering the page credentials you will need to get the source code of the web page. You can get the source code in IE by right clicking on the webpage and selecting "View source". In the source code you will need to find the ID for the username input field and password input field. Once you have obtained these IDs place them in-between the specified .getElementById()s.
The button is accessed by using an index of the form elements. For example, my original index was a (3) because is was the third clickable element in the form. So, you may need to replace the X with a (0) or (1). Optionally you could get the ID of the button and use the .getElementById("ID").click().
Simple way to automate logging into a website using VBscript.
Contact me if you have any questions, comments, or concerns.