Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / VBScript

Opening IE Using C# and Firing Events

Rate me:
Please Sign up or sign in to vote.
3.67/5 (6 votes)
4 Nov 2009CPOL2 min read 120.3K   26   25
How to open Internet Explorer using C# code and fire events in the loaded page.

Introduction

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:

C#
InternetExplorer gmailurl = new InternetExplorer();
object mVal = System.Reflection.Missing.Value;
gmailurl.Navigate("http://www.gmail.com", ref mVal, ref mVal, ref mVal, ref mVal);

HTMLDocument myDoc = new HTMLDocumentClass();
System.Threading.Thread.Sleep(500);
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);
btnsubmit.click();
gmailurl.Visible = true;

In the above code, the class InternetExplorer is from SHDocVw.dll, which you can download from: http://www.dll-files.com/dllindex/dll-files.shtml?shdocvw.

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:

C#
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:

VBScript
Function getgmail()
    Dim count
    Dim ie
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate "http://www.gmail.com"
    Do While ie.Busy Or (ie.READYSTATE <> 4)
        count = count+1
    Loop
    ie.Visible=True
    ie.document.all.username.value = "youruserID"
    ie.document.all.pwd.value = "yourpassword"
    ie.document.all.signIn.click
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! :-)

License

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


Written By
Software Developer (Senior) UST Global
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
wwwx30-Mar-17 20:44
wwwx30-Mar-17 20:44 
QuestionThanks sir Pin
Member 113122643-Feb-16 20:10
Member 113122643-Feb-16 20:10 
QuestionNull reference Errors Pin
Member 1093392115-Jul-14 21:38
Member 1093392115-Jul-14 21:38 
GeneralThanks a lot : Press enter on the browser Pin
Kapil Waghe21-Jan-12 3:31
Kapil Waghe21-Jan-12 3:31 
QuestionConvert to C# Pin
Rungta Atul9-Jan-12 6:24
Rungta Atul9-Jan-12 6:24 
GeneralExample Code for OWA 2010 C# Single Sign On Pin
JimShat9-Aug-11 11:50
JimShat9-Aug-11 11:50 
GeneralMy vote of 2 Pin
JBress9-Nov-09 2:11
JBress9-Nov-09 2:11 
Generalcan't execute Pin
quangnd.edu4-Nov-09 19:39
quangnd.edu4-Nov-09 19:39 
GeneralRe: can't execute Pin
Anoop_K_V4-Nov-09 20:16
Anoop_K_V4-Nov-09 20:16 
GeneralRe: can't execute Pin
Steve Messer5-Nov-09 3:40
Steve Messer5-Nov-09 3:40 
GeneralRe: can't execute Pin
quangnd.edu5-Nov-09 15:24
quangnd.edu5-Nov-09 15:24 
Exception fired here:

myDoc = (HTMLDocument)gmailurl.Document;


And error still is: Error HRESULT E_FAIL has been returned from a call to a COM component.

To smesser: If you run it with winform success, send me a copy of source code Smile | :)

my email: quangnd.edu@hotmail.com

Thanks Smile | :)
GeneralRe: can't execute [modified] Pin
Steve Messer6-Nov-09 19:00
Steve Messer6-Nov-09 19:00 
GeneralRe: can't execute Pin
Steve Messer7-Nov-09 10:03
Steve Messer7-Nov-09 10:03 
GeneralRe: can't execute Pin
DilshadHussain22-Jun-15 2:32
DilshadHussain22-Jun-15 2:32 
GeneralRe: can't execute Pin
Anoop_K_V5-Nov-09 19:56
Anoop_K_V5-Nov-09 19:56 
GeneralRe: can't execute Pin
saurabh915-Nov-09 19:57
saurabh915-Nov-09 19:57 
GeneralRe: can't execute Pin
Anoop_K_V15-Nov-09 21:34
Anoop_K_V15-Nov-09 21:34 
GeneralRe: can't execute Pin
saurabh916-Nov-09 2:01
saurabh916-Nov-09 2:01 
GeneralRe: can't execute Pin
saurabh916-Nov-09 7:03
saurabh916-Nov-09 7:03 
GeneralWhy not to use WATiN or SAHI Pin
Junaid Raza4-Nov-09 2:11
Junaid Raza4-Nov-09 2:11 
GeneralRe: Why not to use WATiN or SAHI Pin
Anoop_K_V4-Nov-09 19:56
Anoop_K_V4-Nov-09 19:56 
GeneralRe: Why not to use WATiN or SAHI Pin
Junaid Raza4-Nov-09 22:00
Junaid Raza4-Nov-09 22:00 
GeneralRe: Why not to use WATiN or SAHI Pin
Anoop_K_V5-Nov-09 1:44
Anoop_K_V5-Nov-09 1:44 
GeneralRe: Why not to use WATiN or SAHI Pin
Junaid Raza5-Nov-09 1:50
Junaid Raza5-Nov-09 1:50 
GeneralIntresting Pin
gaurav_verma_mca4-Nov-09 0:10
gaurav_verma_mca4-Nov-09 0:10 

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.