Click here to Skip to main content
15,899,475 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.8K   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

 
GeneralRe: Server-side automation. Pin
Alex Furman9-Feb-07 5:20
Alex Furman9-Feb-07 5:20 
Questionwhat about catching a html combobox value Pin
genialus5-Nov-06 0:37
genialus5-Nov-06 0:37 
AnswerRe: what about catching a html combobox value Pin
G. Kiran16-Nov-06 22:12
G. Kiran16-Nov-06 22:12 
GeneralWebbrowser control in Access2000 Pin
driftingcowboy15-Oct-06 14:30
driftingcowboy15-Oct-06 14:30 
GeneralTry using WatiN Pin
jvmenen13-Jul-06 22:28
jvmenen13-Jul-06 22:28 
GeneralRe: Try using WatiN [modified] Pin
G. Kiran3-Aug-06 20:27
G. Kiran3-Aug-06 20:27 
Questionproblem while deploying Pin
nitin.mittal10-Jun-06 23:23
nitin.mittal10-Jun-06 23:23 
QuestionLogging in automatically with network login? Pin
ddavison8-Jun-06 5:15
ddavison8-Jun-06 5:15 
AnswerRe: Logging in automatically with network login? Pin
G. Kiran8-Jun-06 19:40
G. Kiran8-Jun-06 19:40 
GeneralRe: Logging in automatically with network login? [modified] Pin
ddavison9-Jun-06 5:33
ddavison9-Jun-06 5:33 
GeneralRe: Logging in automatically with network login? Pin
G. Kiran12-Jun-06 22:03
G. Kiran12-Jun-06 22:03 
GeneralIdentifying Controls Pin
docrob16-Jun-06 9:16
docrob16-Jun-06 9:16 
GeneralRe: Identifying Controls Pin
G. Kiran6-Jun-06 19:44
G. Kiran6-Jun-06 19:44 
GeneralRe: Identifying Controls Pin
docrob17-Jun-06 3:53
docrob17-Jun-06 3:53 
GeneralVery interested..! Pin
hugobq1-Jun-06 12:07
hugobq1-Jun-06 12:07 
GeneralHoly cow Pin
NormDroid19-May-06 3:34
professionalNormDroid19-May-06 3:34 
GeneralRe: Holy cow [modified] Pin
G. Kiran1-Jun-06 0:45
G. Kiran1-Jun-06 0:45 
GeneralRe: Holy cow [modified] Pin
CT37-Jun-06 0:01
CT37-Jun-06 0:01 

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.