Click here to Skip to main content
12,451,446 members (56,685 online)
Click here to Skip to main content
Add your own
alternative version

Stats

189K views
44 bookmarked
Posted

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

, 16 Jun 2006 CPOL
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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralDealing with multiple frames and frameset Pin
stephen32327-Jan-10 20:54
memberstephen32327-Jan-10 20:54 
GeneralOr the better is: Pin
polymorphism11-Jun-09 5:37
memberpolymorphism11-Jun-09 5:37 
QuestionBug fixing Pin
venkat26854-Jun-09 20:52
membervenkat26854-Jun-09 20:52 
QuestionHow to active the NavigateComplete2 events? Pin
zylaoer15-May-09 22:23
memberzylaoer15-May-09 22:23 
GeneralTake a screenshot of Internet Explorer browser with C# Pin
donchev_p29-Oct-08 23:42
memberdonchev_p29-Oct-08 23:42 
QuestionIs it possible to use the input type=file ? Pin
validator2323-Oct-08 9:10
membervalidator2323-Oct-08 9:10 
QuestionCan't get PG&E website to Submit Pin
MasonB40425-Sep-08 13:33
memberMasonB40425-Sep-08 13:33 
GeneralError while converting the com to interface Pin
ashiclt17-Aug-08 20:39
memberashiclt17-Aug-08 20:39 
GeneralRe: Error while converting the com to interface Pin
Member 260082618-Sep-08 22:39
memberMember 260082618-Sep-08 22:39 
GeneralCOM Error on Document Object Pin
SGrind18-Jul-08 17:28
memberSGrind18-Jul-08 17:28 
GeneralRe: COM Error on Document Object Pin
palmerm120-May-09 10:59
memberpalmerm120-May-09 10:59 
GeneralRe: COM Error on Document Object Pin
palmerm120-May-09 11:06
memberpalmerm120-May-09 11:06 
GeneralRe: COM Error on Document Object Pin
Member 350908021-Feb-10 13:36
memberMember 350908021-Feb-10 13:36 
GeneralRe: COM Error on Document Object Pin
palmerm122-Feb-10 5:16
memberpalmerm122-Feb-10 5:16 
GeneralRe: COM Error on Document Object Pin
Member 350908022-Feb-10 15:27
memberMember 350908022-Feb-10 15:27 
GeneralImplement for C# Pin
Nitin Sawant6-Jun-08 22:22
memberNitin Sawant6-Jun-08 22:22 
GeneralRe: Implement for C# Pin
Member 350908022-Feb-10 15:31
memberMember 350908022-Feb-10 15:31 
QuestionDownload file Pin
Member 277691918-Mar-08 23:05
memberMember 277691918-Mar-08 23:05 
QuestionAutomate Javascript Page Pin
vishnubhushan6-Sep-07 22:08
membervishnubhushan6-Sep-07 22:08 
AnswerRe: Automate Javascript Page Pin
Alex Furman7-Nov-07 15:15
memberAlex Furman7-Nov-07 15:15 
GeneralElement click Pin
Sajudeen Kassim27-Aug-07 20:06
memberSajudeen Kassim27-Aug-07 20:06 
GeneralOutdated Webpage but not InternetExplorer object Pin
metramo21-Jun-07 3:13
membermetramo21-Jun-07 3:13 
Questionhow to passing information to a already opened web page? Pin
Maximiline3-Apr-07 19:38
memberMaximiline3-Apr-07 19:38 
GeneralServer-side automation. Pin
Javalsu16-Nov-06 5:35
memberJavalsu16-Nov-06 5:35 
GeneralRe: Server-side automation. Pin
G. Kiran16-Nov-06 22:19
memberG. Kiran16-Nov-06 22:19 
GeneralRe: Server-side automation. Pin
Alex Furman9-Feb-07 5:20
memberAlex Furman9-Feb-07 5:20 
Questionwhat about catching a html combobox value Pin
genialus5-Nov-06 0:37
membergenialus5-Nov-06 0:37 
AnswerRe: what about catching a html combobox value Pin
G. Kiran16-Nov-06 22:12
memberG. Kiran16-Nov-06 22:12 
GeneralWebbrowser control in Access2000 Pin
driftingcowboy15-Oct-06 14:30
memberdriftingcowboy15-Oct-06 14:30 
GeneralTry using WatiN Pin
jvmenen13-Jul-06 22:28
memberjvmenen13-Jul-06 22:28 
GeneralRe: Try using WatiN [modified] Pin
G. Kiran3-Aug-06 20:27
memberG. Kiran3-Aug-06 20:27 
Questionproblem while deploying Pin
nitin.mittal10-Jun-06 23:23
membernitin.mittal10-Jun-06 23:23 
QuestionLogging in automatically with network login? Pin
ddavison8-Jun-06 5:15
memberddavison8-Jun-06 5:15 
AnswerRe: Logging in automatically with network login? Pin
G. Kiran8-Jun-06 19:40
memberG. Kiran8-Jun-06 19:40 
GeneralRe: Logging in automatically with network login? [modified] Pin
ddavison9-Jun-06 5:33
memberddavison9-Jun-06 5:33 
GeneralRe: Logging in automatically with network login? Pin
G. Kiran12-Jun-06 22:03
memberG. Kiran12-Jun-06 22:03 
GeneralIdentifying Controls Pin
docrob16-Jun-06 9:16
memberdocrob16-Jun-06 9:16 
GeneralRe: Identifying Controls Pin
G. Kiran6-Jun-06 19:44
memberG. Kiran6-Jun-06 19:44 
GeneralRe: Identifying Controls Pin
docrob17-Jun-06 3:53
memberdocrob17-Jun-06 3:53 
GeneralVery interested..! Pin
hugobq1-Jun-06 12:07
memberhugobq1-Jun-06 12:07 
GeneralHoly cow Pin
norm.net19-May-06 3:34
membernorm.net19-May-06 3:34 
GeneralRe: Holy cow [modified] Pin
G. Kiran1-Jun-06 0:45
memberG. Kiran1-Jun-06 0:45 
GeneralRe: Holy cow [modified] Pin
CT37-Jun-06 0:01
memberCT37-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 17 Jun 2006
Article Copyright 2006 by G. Kiran
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid