Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Visual Basic

Automation of Internet Explorer Using shdocvw.dll and mshtml.tlb – A Case Study

Rate me:
Please Sign up or sign in to vote.
2.97/5 (15 votes)
16 Jun 2006CPOL 262.1K   46   43
Automation of Internet Explorer using shdocvw.dll and mshtml.dll.

Introduction

Microsoft Internet Explorer comes with a fairly comprehensive, although sparsely documented, Object Model. If you've used the Web Browser control in Access, you are already familiar with the capabilities of IE's Object Model. All of the functionality in IE's object model (not counting external support, like scripting support etc.) is provided by the following two DLLs:

  • shdocvw.dll (Microsoft Internet Controls)
  • mshtml.tlb (Microsoft HTML Object Library)

You can automate IE to save an HTML file locally, inspect all the elements, and parse out a particular item at runtime.

Here's some sample code that automates through Internet Explorer Windows login into rediffmail.com, if the user name and password are valid.

First, the application opens the http://rediff.com site. It types the user name and password at the specified location and clicks the Submit button so that it goes to the Inbox page. It also opens the Compose page for the particular user.

The application extensively uses the shdocvw.InternetExplorer object and the mshtml.Document object.

VB
Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
    Dim wbBrowser As New SHDocVw.InternetExplorer wbBrowser.Visible = True
    wbBrowser.Navigate("http://www.rediff.com", Nothing, Nothing, Nothing, Nothing)
    Do
    Loop Until Not wbBrowser.Busy
    LoginIntoSite(wbBrowser)
    OpennComposePage(wbBrowser)
End Sub

Public Sub LoginIntoSite(ByRef wbBrowser As SHDocVw.InternetExplorer)

    Dim HTMLDoc As mshtml.HTMLDocument

    Do
    Loop Until Not wbBrowser.Busy

    HTMLDoc = wbBrowser.Document

    Dim iHTMLCol As IHTMLElementCollection
    Dim iHTMLEle As IHTMLElement
    Dim str, userName, passwd As String
    
    iHTMLCol = HTMLDoc.getElementsByTagName("input")
    
    ' Type the user name in the username text box
    For Each iHTMLEle In iHTMLCol
        If Not iHTMLEle.getAttribute("name") Is Nothing Then
            str = iHTMLEle.getAttribute("name").ToString
            If str = "login" Then
                iHTMLEle.setAttribute("value", "<VALID USER NAME>")
                Exit For
            End If
        End If
    Next
    
    ' Type the password in the password text box
    For Each iHTMLEle In iHTMLCol
        If Not iHTMLEle.getAttribute("name") Is Nothing Then
            str = iHTMLEle.getAttribute("name").ToString
            If str = "passwd" Then
                iHTMLEle.setAttribute("value", "<VALID PASSWORD>")
                Exit For
            End If
        End If
    Next

    ' Press the submit button
    For Each iHTMLEle In iHTMLCol
        If Not iHTMLEle.getAttribute("name") Is Nothing Then
            If iHTMLEle.outerHTML = "<INPUT type=image" & _ 
                   " height=21 width=26 " & _ 
                   "src=""http://im.rediff.com/" & _ 
                   "uim/rm_go_but.gif"" border=0>" Then
                iHTMLEle.click()
                Exit For
            End If
        End If
    Next

    Do
    Loop Until Not wbBrowser.Busy
End Sub

Public Sub OpenComposePage(ByRef wbBrowser As SHDocVw.InternetExplorer)

    Dim HTMLDoc1 As mshtml.HTMLDocument
    Dim iHtmlCol As IHTMLElementCollection
    Dim iHtmlEle As IHTMLElement

    Do
    Loop Until Not wbBrowser.Busy

    HTMLDoc1 = mshtml.HTMLDocument

    iHtmlCol = HTMLDoc1.getElementsByTagName("a")
    ' Press the anchor tag to open compose page
    For Each iHtmlEle In iHtmlCol
        If Not iHtmlEle.outerText Is Nothing Then
            If iHtmlEle.outerText.ToLower = "write mail".ToLower Then
                iHtmlEle.click()
                Exit For
            End If
        End If
    Next

    Do
    Loop Until Not wbBrowser.Busy
End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
I have been working as Application Engineer for 2+ years in ASP.NET

Comments and Discussions

 
GeneralDealing with multiple frames and frameset Pin
stephen32327-Jan-10 20:54
stephen32327-Jan-10 20:54 
GeneralOr the better is: Pin
polymorphism11-Jun-09 5:37
polymorphism11-Jun-09 5:37 
QuestionBug fixing Pin
venkat26854-Jun-09 20:52
venkat26854-Jun-09 20:52 
QuestionHow to active the NavigateComplete2 events? Pin
zylaoer15-May-09 22:23
zylaoer15-May-09 22:23 
GeneralTake a screenshot of Internet Explorer browser with C# Pin
donchev_p29-Oct-08 23:42
donchev_p29-Oct-08 23:42 
QuestionIs it possible to use the input type=file ? Pin
validator2323-Oct-08 9:10
validator2323-Oct-08 9:10 
QuestionCan't get PG&E website to Submit Pin
MasonB40425-Sep-08 13:33
MasonB40425-Sep-08 13:33 
GeneralError while converting the com to interface Pin
PadiyathDeepak17-Aug-08 20:39
PadiyathDeepak17-Aug-08 20:39 
GeneralRe: Error while converting the com to interface Pin
Member 260082618-Sep-08 22:39
Member 260082618-Sep-08 22:39 
You need Office PIA to install at yours machine
GeneralCOM Error on Document Object Pin
SGrind18-Jul-08 17:28
SGrind18-Jul-08 17:28 
GeneralRe: COM Error on Document Object Pin
marvinpjr20-May-09 10:59
marvinpjr20-May-09 10:59 
GeneralRe: COM Error on Document Object Pin
marvinpjr20-May-09 11:06
marvinpjr20-May-09 11:06 
GeneralRe: COM Error on Document Object Pin
navyjax221-Feb-10 13:36
navyjax221-Feb-10 13:36 
GeneralRe: COM Error on Document Object Pin
marvinpjr22-Feb-10 5:16
marvinpjr22-Feb-10 5:16 
GeneralRe: COM Error on Document Object Pin
navyjax222-Feb-10 15:27
navyjax222-Feb-10 15:27 
GeneralImplement for C# Pin
Nitin S6-Jun-08 22:22
professionalNitin S6-Jun-08 22:22 
GeneralRe: Implement for C# Pin
navyjax222-Feb-10 15:31
navyjax222-Feb-10 15:31 
QuestionDownload file Pin
JoshMachin18-Mar-08 23:05
JoshMachin18-Mar-08 23:05 
QuestionAutomate Javascript Page Pin
vishnubhushan6-Sep-07 22:08
vishnubhushan6-Sep-07 22:08 
AnswerRe: Automate Javascript Page Pin
Alex Furman7-Nov-07 15:15
Alex Furman7-Nov-07 15:15 
GeneralElement click Pin
Sajudeen Kassim27-Aug-07 20:06
Sajudeen Kassim27-Aug-07 20:06 
GeneralOutdated Webpage but not InternetExplorer object Pin
metramo21-Jun-07 3:13
metramo21-Jun-07 3:13 
Questionhow to passing information to a already opened web page? Pin
Maximiline3-Apr-07 19:38
Maximiline3-Apr-07 19:38 
GeneralServer-side automation. Pin
Javalsu16-Nov-06 5:35
Javalsu16-Nov-06 5:35 
GeneralRe: Server-side automation. Pin
G. Kiran16-Nov-06 22:19
G. Kiran16-Nov-06 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.