- Internet Explorer Automation
The Class Explorer Provides very basic set of operations on Internet Explorer. Before use this class you have to add c:\windows\system32\shdocvw.dll , c:\windows\system32\mshtml.tlb files as references of the project.
The main program given below can be used to test this class.
download the project (Please change th extention to .zip after downloading.
//Explorer Class
using System;
using System.Collections.Generic;
using System.Text;
using SHDocVw;
using mshtml;
namespace IEAutomationFS
{
/*
* c:\windows\system32\shdocvw.dll
* c:\windows\system32\mshtml.tlb
*
* as references.
*/
public class Explorer
{
private InternetExplorer explorer;
private HTMLDocument document;
/// <summary>
/// create new Internet Explorer instance and show it
/// </summary>
public Explorer()
{
explorer = new InternetExplorer();
explorer.Visible = true;
explorer.NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler (explorer_NavigateComplete2);
}
/// <summary>
/// Fires when Navigation Completed
/// </summary>
/// <param name="URL">The URL.</param>
private void explorer_NavigateComplete2 (object pDisp, ref object URL)
{
this.document = (HTMLDocument) explorer.Document;
NavigateCompleteEvent ((HTMLDocument)this.document, URL.ToString());
}
/// <summary>
/// navigate to given url
/// </summary>
public void Navigate(string url)
{
object empty = string.Empty;
explorer.Navigate (url,ref empty,ref empty,ref empty,ref empty);
}
/// <summary>
/// Get the current html Document
/// </summary>
public HTMLDocument Document
{
get
{
if (document == null)
{
return null;
}
return document;
}
}
public delegate void NavigateCompleteEventHandler (HTMLDocument document, string url);
public event NavigateCompleteEventHandler NavigateCompleteEvent;
}
}
//main Program to test the explorer class
using System;
using System.Collections.Generic;
using System.Text;
using mshtml;
namespace IEAutomationFS
{
class Program
{
static void Main(string[] args)
{
//create new Explorer instance
Explorer explorer = new Explorer();
//bind the Navigation Complete Event
explorer.NavigateCompleteEvent += new Explorer.NavigateCompleteEventHandler (explorer_NavigateCompleteEvent);
//navigate to a url
explorer.Navigate("http://freesnippets.co.nr/");
//wait until user press a key
Console.ReadKey();
}
static void explorer_NavigateCompleteEvent (mshtml.HTMLDocument document, string url)
{
Console.WriteLine("Navigation Completed");
Console.WriteLine("URL = " + url);
HTMLInputTextElement box = (HTMLInputTextElement)document.getElementById("q");
box.value = "Internet Explorer Automation";
HTMLInputButtonElement button = (HTMLInputButtonElement)document.getElementById("sbb");
button.click();
}
}
}
|