Click here to Skip to main content
Click here to Skip to main content

Opening IE Using C# and Firing Events

, 4 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
How to open Internet Explorer using C# code and fire events in the loaded page.


This article shows code to open a website in a new Internet Explorer window and find the controls inside the website and fire the events in that site.

Using the Code

Recently, I came across a requirement for opening a website in Internet Explorer and firing the events in that site (e.g., open GMail and fill the user ID and password and fire the sign-in button event) using C#. It was quite interesting.... The code I used is shown below:

InternetExplorer gmailurl = new InternetExplorer();
object mVal = System.Reflection.Missing.Value;
gmailurl.Navigate("", ref mVal, ref mVal, ref mVal, ref mVal);

HTMLDocument myDoc = new HTMLDocumentClass();
myDoc = (HTMLDocument)gmailurl.Document;
HTMLInputElement userID = (HTMLInputElement)myDoc.all.item("username", 0);
userID.value = "youruserid";
HTMLInputElement pwd = (HTMLInputElement)myDoc.all.item("pwd", 0);
pwd.value = "yourpassword";
HTMLInputElement btnsubmit = (HTMLInputElement)myDoc.all.item("signIn", 0);;
gmailurl.Visible = true;

In the above code, the class InternetExplorer is from SHDocVw.dll, which you can download from:

Another reference I have used is for the HTMLDocument class is MSHTML, which you can get from a COM reference (right click the project in Solution Explorer and click on Add Reference, go to the COM tab, and add Microsoft HTML Object Library).

In the above code:

HTMLInputElement userID = (HTMLInputElement)myDoc.all.item("username", 0);

is used to find the textbox for the username, where the "username" is nothing but the name of the input textbox control which is used in the GMail site. You can find this by going to the View Source of the GMail site; similarly for the password and the sign-in button.

The code which above will open Internet Explorer on the server side and not on the client side, so I went for a client-script which will do the same job as above. Here is the VBScript code I used:

Function getgmail()
    Dim count
    Dim ie
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate ""
    Do While ie.Busy Or (ie.READYSTATE <> 4)
        count = count+1
    ie.document.all.username.value = "youruserID"
    ie.document.all.pwd.value = "yourpassword"
END Function

Points of Interest

I have tried the above solution in many ways; I tried to use ProcessStartInfo, IHTMLDocument, and then the HttpWebRequest class, which didn't satisfied my requirements. Another point which I have noted is in some sites, for some controls, there will not be any name (e.g., username, pwd, signIn ...) property, so I had to struggle a little to find controls, and I found that using the unique IDs for each control used in that site helps.

Hope this helps someone...... Happy Coding! Smile | :)


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


About the Author

Software Developer (Senior) UST Global
India India
No Biography provided

Comments and Discussions

QuestionNull reference Errors PinmemberMember 1093392115-Jul-14 22:38 
GeneralThanks a lot : Press enter on the browser PinmemberKapilWaghe21-Jan-12 4:31 
QuestionConvert to C# Pinmemberatulrungta9-Jan-12 7:24 
GeneralExample Code for OWA 2010 C# Single Sign On PinmemberJimShat9-Aug-11 12:50 
GeneralMy vote of 2 Pinmembergg679-Nov-09 3:11 
Generalcan't execute PinmemberMember 66880694-Nov-09 20:39 
GeneralRe: can't execute PinmemberAnoop_K_V4-Nov-09 21:16 
GeneralRe: can't execute Pinmembersmesser5-Nov-09 4:40 
The names he uses for username and password aren't correct. View the source of the google login page to see the correct names.
I don't recall off the top of my head as I tried this at home and I am now at work.
But I believe for me passwd instead of pwd worked I can't remember on the other input.
Some exception handling would be in order. The error is because it can't find a control of the given name. Localization??
PS: I used it from a winform just fine.
GeneralRe: can't execute Pinmemberquangnd.edu5-Nov-09 16:24 
GeneralRe: can't execute [modified] Pinmembersmesser6-Nov-09 20:00 

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 | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 4 Nov 2009
Article Copyright 2009 by Anoop_K_V
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid