So today we are going to talk about Automating user activities on Internet Explorer, but this time using HTML instead of Sendkeys. Just like with QTP/UFT software testing tools, we are going to directly put the user input straight into the HTML code of a website.
Let’s start with a base-script that simply opens the Internet Explorer Application and navigates to the Facebook.com. Our goal would be to login to Facebook.
Option Explicit Dim ie, ipf Set ie = CreateObject("InternetExplorer.Application") Sub WaitForLoad Do While IE.Busy WScript.Sleep 500 Loop End Sub ie.Left = 0 ie.Top = 0 ie.Toolbar = 0 ie.StatusBar = 0 ie.Height = 200 ie.Width = 1020 ie.Resizable = 0 ie.Navigate "https://www.facebook.com/" Call WaitForLoad ie.Visible = True ie.Document.All.Item("")
So this is essentially the same code used in the last tutorial, except for a few differences. The minor difference is that a subroutine is used to simplify telling the VbScript to wait until the webpage has loaded. And the major difference is the introduction of the Document.All.Item(“”) function, which allows you to put your information right into the input fields of a webpage. To use Document.All.Item(“”), you first need to go to the webpage that has the input field that you want to put your information in.
Right-Click and select View this Page Source (Is available for Firefox; IE browser doesn’t have it?).
Hold Ctrl and the F key to open the find search box.
Remember how there is some text near the input fields that we want to work with, like email or phone, password?
Search those terms in the find search box. Let’s start with “Email or Phone”
Next, keep scrolling to the right until you find something called id=”email”. That id is what we are looking for to identify the input field of Email or Phone. →
<label for="email">Email or Phone</label></td><td class="html7magic"><label for="pass">Password</label></td></tr><tr><td><input type="email" class="inputtext" name="email" id="email" value="" tabindex="1" />
In order to identify the input field of Email or Phone, put the email id in ie.Document.All.Item(“”). In order to input information into the field, add .value=”” to the end of the code with the information-string in between the Quotations.
. . . ie.Document.All.Item("email").Value="[email protected]"
The process of identifying and filling the password input field is similar. Again, find the id for Password. To find it, scroll right from where you found the id for Email or Password. Look for something called input type=”password”, the id for Password should be near it.
<input type="password" class="inputtext" name="pass" id="pass" tabindex="2" />
Copy same code, but replace the id and value.
. . . ie.Document.All.Item("email").Value="[email protected]" ie.Document.All.Item("pass").Value="ThePassword"
So far, this bit of code allows you to fill the email and password fields. The final step is to tell the VbScript to click or enter the Log In button. To do this, first search for the word form in the Page Source. Due to the nature of HTML code, it may help to include a wicket < in front of the word form, like this <form.
<form id="login_form" action="https://www.facebook.com/login.php?login_attempt=1&lwv=110" method="post" novalidate="1" onsubmit="return window.Event && Event.__inlineSubmit && Event.__inlineSubmit(this,event)">
Like before, add the id login_form in ie.Document.All.Item(“”). But at the end include the code .submit, which tells the VbScript to click or enter the Log In button.
. . . ie.Document.All.Item("email").Value="[email protected]" ie.Document.All.Item("pass").Value="ThePassword" ie.Document.All.Item("login_form").Submit
- What you see happen on the Internet Explorer (IE) browser-application is dependent on where you put ie.Visible=True . Putting it at the end of the script only shows you the end result, whereas putting it near the beginning of the script will show you all the processes that the IE application goes through, like inputting strings into the fields and logging in.
- You can make the IE browser full screen by adding this code: ie.FullScreen=True
- To make your script continue running after an error, add the code: On Error Resume Next . In the context of logging into Facebook, you will run into an error if you don’t log out of Facebook. But if you want the script to continue running regardless, you would add that above code to a separate line in your script near the top.
- Note that if you use the QTP testing tool, searching for id on a webpage is made a lot easier if you use Object Spy.