We will see how we can automate different
html controls using C#.Net.
Step 1: Create C# Windows Library Project in Visual Studio.
Step 2: Add one text box and one button in Form. We will enter this text from text box into Google search text box.
Step 3: Add “Microsoft Internet Controls” and “Microsoft Object Library” references from COM tab. It will add SHDocVw and MSHTML in Reference list of our .net application.
SHDocVw class is used to launch Internet Explorer and handle its different events. One of the event we will be using is DocumentComplete event.
MSHTML class is used to automate different types of html controls like textbox, button, radio button, checkbox, dropdown, etc.
Step 4: Launch Internet Explorer.
Create
an object of SHDocVw.InternetExplorer class. Register DocumentComplete event of
this object and navigate to www.google.com
website using this object.
SHDocVw.InternetExplorer ie = null;
Following code will help you to understand how you can find any html element from document and how you can set any text box value and click on button.
I will show one example of automating Google
website. We will enter text in search text box of Google page and we will press
enter. These two actions will be performed using our C#.Net code.
Step 1: Create C# Windows Library Project in Visual Studio.
Step 2: Add one text box and one button in Form. We will enter this text from text box into Google search text box.
Step 3: Add “Microsoft Internet Controls” and “Microsoft Object Library” references from COM tab. It will add SHDocVw and MSHTML in Reference list of our .net application.
SHDocVw class is used to launch Internet Explorer and handle its different events. One of the event we will be using is DocumentComplete event.
MSHTML class is used to automate different types of html controls like textbox, button, radio button, checkbox, dropdown, etc.
Step 4: Launch Internet Explorer.
SHDocVw.InternetExplorer ie = null;
private void
button1_Click(object sender, EventArgs e)
{
ie = new
SHDocVw.InternetExplorer();
ie.DocumentComplete
+= new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
ie.Navigate("www.google.com", Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
ie.Visible
= true;
}
Above code will launch Google in Internet
Explorer. Document Complete event of an object will be called once webpage is
completely loaded.
Step 5: Handle Document Complete event
We can automate html controls of a webpage using
this event.
Get Internet Explorer document using IHTMLDocument2
class object. Find Google search text box and search button elements using its
ids from this document.
Following code will help you to understand how you can find any html element from document and how you can set any text box value and click on button.
void ie_DocumentComplete(object
pDisp, ref object
URL)
{
if (Convert.ToString(URL).Contains("www.google.co"))
{
//Get HTML document
mshtml.IHTMLDocument2 doc =
ie.Document as mshtml.IHTMLDocument2;
//Get google textbox and enter text in that
textbox
mshtml.HTMLInputElement googleTextbox
= doc.all.item("q", Type.Missing) as
mshtml.HTMLInputElement;
googleTextbox.value = textBox1.Text;
//Get google search button and click on that
button
mshtml.HTMLButtonElement googleSearchbutton
= doc.all.item("btnK", Type.Missing) as
mshtml.HTMLButtonElement;
googleSearchbutton.click();
}
}
Now, let’s see output of above coding.
Output Internet Explorer.
This is a very simple example. You can automate
different html elements using mshtml class.
No comments:
Post a Comment