InternetExplorer automation using VBScript

Posted on 2006-05-24
Last Modified: 2008-01-09

I need to fill a long online form with lots of data,so I want to do it programmatically using VBScript.
The form webpage initially has a login form, than you get to an index page (selecting one of several forms) and finally to the form page.

I can manage to automate the login access and navigation up to the form page, but
1) how can I retrieve the full HTML text of the form page (I need this because in order to match the form input box with the data I want to enter, I have to analyze the text surrounding each input box)
2) how can I submit a form that doesn't have a "submit" button (the form gets submitted when you click on a link that actually does "form.submit" in javascript). I tried oIE.document.all("form1").submit, but I get an error saying that the submit method doesn't exist.

This is the code I'm using (working, but I changed the real address/credentials with fake ones) to reach to the form:

Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Visible = True
oIE.Navigate "\somepage"
Do While oIE.Busy Or (oIE.READYSTATE <> 4)
    Wscript.Sleep 10

If Not oIE.document.all("user") is nothing Then
 oIE.document.all("user").Value = "pippo"
 oIE.document.all("pass").Value = "baudo"
 Do While oIE.Busy Or (oIE.READYSTATE <> 4)
     Wscript.Sleep 10
End If

'navigate to form A01
oIE.Navigate ""
Do While oIE.Busy Or (oIE.READYSTATE <> 4)
    Wscript.Sleep 10

'Now I see the form on the screen and I need to retrieve the full HTML source
Question by:lbertacco
    LVL 35

    Expert Comment

    Hi lbertacco,

    For the html source, use:

    For the submit part, hopefully someone else can help you with that, but have you tried something like
     oIE.Navigate "javascript:form.submit()"

    Just a thought
    LVL 11

    Author Comment

    I'll try that. Can you also point me to a good online reference the InternetExplorer.Document object?
    LVL 35

    Expert Comment

    As far as looking at the properties/methods, I open an MS office program, go to the VBA (alt-f11), and add a reference to "Microsoft HTML Object Library", and then Dim a variable as HTMLDocument (this works for other objects of the internetexplorer object too).  But you can see how to use all of the IE object, not just the document, at
    LVL 35

    Expert Comment

    From there (sorry, forgot to include this), you can see the document object at
    LVL 11

    Author Comment

    Mmh, the innerHTML property doesn't return the orignal HTML but a bit reformatted (capitalized tags, removed quotes, reordered attributes).
    Is there a way to obtain the ORIGINAL html code?
    LVL 35

    Accepted Solution

    Unfortunately, I don't think there is using the IE object, since that is how it is processed by the app.  You could try using the xmlhttp object, though you'll have to experiment with the logging in portion.  Perhaps once you log in with IE, and keep that object around, it may work for you:

    Function GetWebText(ByVal vWebSite)
     Dim oXMLHTTP, vWebText, i
     Set oXMLHTTP = CreateObject("msxml2.xmlhttp")
     oXMLHTTP.Open "GET", vWebSite, False
     If (oXMLHTTP.readyState = 4) And (oXMLHTTP.Status = 200) Then
      vWebText = oXMLHTTP.ResponseText
      vWebText = Replace(vWebText, "&quot;", Chr(34))
      vWebText = Replace(vWebText, "&lt;", Chr(60))
      vWebText = Replace(vWebText, "&gt;", Chr(62))
      vWebText = Replace(vWebText, "&amp;", Chr(38))
      vWebText = Replace(vWebText, "&nbsp;", Chr(32))
      For i = 1 To 255
       vWebText = Replace(vWebText, "&#" & i & ";", Chr(i))
     End If
     GetWebText = vWebText
     Set oXMLHTTP = Nothing
    End Function

    You could also try the .Open line with the un/pw:

     oXMLHTTP.Open "GET", vWebSite, False, "username", "password"

    But that is avoiding IE altogether (won't work for the forms you need to use).  Unfortunately, I don't believe there is a property of the IE object to get the original html.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    Suggested Solutions

    Title # Comments Views Activity
    userCompare  challenge 3 54
     shows up in Outlook, not OWA or on phone 3 45
    strCount chalenge 3 34
    Path of Workbook 3 30
    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    This is an explanation of a simple data model to help parse a JSON feed
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now