Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 16 Jun 2006
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

G. Kiran
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 Pinmemberstephen32327-Jan-10 20:54 
GeneralOr the better is: Pinmemberpolymorphism11-Jun-09 5:37 
QuestionBug fixing Pinmembervenkat26854-Jun-09 20:52 
QuestionHow to active the NavigateComplete2 events? Pinmemberzylaoer15-May-09 22:23 
GeneralTake a screenshot of Internet Explorer browser with C# Pinmemberdonchev_p29-Oct-08 23:42 
QuestionIs it possible to use the input type=file ? Pinmembervalidator2323-Oct-08 9:10 
QuestionCan't get PG&E website to Submit PinmemberMasonB40425-Sep-08 13:33 
GeneralCOM Error on Document Object PinmemberSGrind18-Jul-08 17:28 
GeneralRe: COM Error on Document Object Pinmemberpalmerm120-May-09 10:59 
GeneralRe: COM Error on Document Object Pinmemberpalmerm120-May-09 11:06 
GeneralRe: COM Error on Document Object PinmemberMember 350908021-Feb-10 13:36 
GeneralRe: COM Error on Document Object Pinmemberpalmerm122-Feb-10 5:16 
GeneralRe: COM Error on Document Object PinmemberMember 350908022-Feb-10 15:27 
GeneralImplement for C# PinmemberNitin Sawant6-Jun-08 22:22 
GeneralRe: Implement for C# PinmemberMember 350908022-Feb-10 15:31 
QuestionDownload file PinmemberMember 277691918-Mar-08 23:05 
QuestionAutomate Javascript Page Pinmembervishnubhushan6-Sep-07 22:08 
AnswerRe: Automate Javascript Page PinmemberAlex Furman7-Nov-07 15:15 
GeneralElement click PinmemberSajudeen Kassim27-Aug-07 20:06 
GeneralOutdated Webpage but not InternetExplorer object Pinmembermetramo21-Jun-07 3:13 
Questionhow to passing information to a already opened web page? PinmemberMaximiline3-Apr-07 19:38 
GeneralServer-side automation. PinmemberJavalsu16-Nov-06 5:35 
GeneralRe: Server-side automation. PinmemberG. Kiran16-Nov-06 22:19 
GeneralRe: Server-side automation. PinmemberAlex Furman9-Feb-07 5:20 
Questionwhat about catching a html combobox value Pinmembergenialus5-Nov-06 0:37 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 17 Jun 2006
Article Copyright 2006 by G. Kiran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid