using System; using System.Collections.Generic; using System.Linq; using System.Text; using mshtml; using SHDocVw; using System.Threading; using System.IO; using System.Windows.Forms; namespace IEAutomationSample { class Program { static void Main(string[] args) { //Run the JavaScript event example. bool runJavaScriptExample = false; if (runJavaScriptExample) { JavaScriptEventExample(); Environment.Exit(0); } InternetExplorer ieBrowser = new InternetExplorerClass(); ieBrowser.Visible = true; NavigateToUrl(ieBrowser, "http://www.dogpile.com"); Thread.Sleep(5000); //Do stuff here... HTMLDocument htmlDoc = (HTMLDocument)ieBrowser.Document; string pageTitle = htmlDoc.title; Console.WriteLine(pageTitle); IHTMLInputTextElement textBox = (IHTMLInputTextElement)htmlDoc.getElementById("icePage_SearchBoxTop_qkw"); textBox.value = "powered by awesome"; HTMLInputButtonElement submitButton = (HTMLInputButtonElement)htmlDoc.getElementById("icePage_SearchBoxTop_qkwsubmit"); submitButton.click(); Thread.Sleep(5000); htmlDoc = (HTMLDocument)ieBrowser.Document; Console.WriteLine("Links by tag name:"); foreach(IHTMLElement anchorElement in htmlDoc.getElementsByTagName("a")) { if(anchorElement.className == "resultLink") { Console.WriteLine(anchorElement.innerText); } } Console.WriteLine("Links by result walking:"); IHTMLElement resultContainerDiv = htmlDoc.getElementById("icePage_SearchResults_ResultsRepeaterByRelevance_ResultRepeaterContainerWeb"); foreach (HTMLDivElement resultDiv in (IHTMLElementCollection)resultContainerDiv.children) { IHTMLElement resultLink = (IHTMLElement)resultDiv.firstChild; Console.WriteLine(resultLink.innerText); } Console.WriteLine("img src:"); foreach (IHTMLImgElement imgElement in htmlDoc.getElementsByTagName("img")) { Console.WriteLine(imgElement.src); } Console.WriteLine("img src 2:"); foreach (IHTMLImgElement imgElement in htmlDoc.images) { Console.WriteLine(imgElement.src); } foreach (HTMLDivElement divElement in htmlDoc.getElementsByTagName("div")) { divElement.runtimeStyle.borderStyle = "groove"; divElement.runtimeStyle.borderWidth = "3"; } Thread.Sleep(5000); ieBrowser.Quit(); } public static void NavigateToUrl(InternetExplorer ieBrowser, string url) { object nullObject = null; ieBrowser.Navigate(url, ref nullObject, ref nullObject, ref nullObject, ref nullObject); } public static void JavaScriptEventExample() { InternetExplorer ieBrowser = new InternetExplorerClass(); ieBrowser.Visible = true; string localUrl = Path.Combine(Environment.CurrentDirectory, "JavaScriptSample.html"); NavigateToUrl(ieBrowser, localUrl); Thread.Sleep(1000); HTMLDocument htmlDoc = (HTMLDocument)ieBrowser.Document; HTMLInputTextElement textBox = (HTMLInputTextElement)htmlDoc.getElementById("textbox"); textBox.value = "powered by awesome"; HTMLInputButtonElement submitButton = (HTMLInputButtonElement)htmlDoc.getElementById("submitbutton"); submitButton.click(); Thread.Sleep(5000); textBox.focus(); SendKeys.SendWait("this triggers keypress/onchange"); submitButton.focus(); Thread.Sleep(5000); ieBrowser.Quit(); } } }