How can i control IE using C#?
Posted in Help the coder! on Jun 14, 2009 at 18:45 IST (about 1 year ago). Subscribe to this post
Email
Showing comments 1 to 4 of total 4 on page 1 of 1
Post replyShowing comments 1 to 4 of total 4 on page 1 of 1
« Previous1Next »
sulphiRank: 434
How can I control IE using C#? Using COM one can do all sorts of interesting stuff, but there doesn't seem to be that many methods. For example, how can I force a button to be clicked?
In general, how can I individually control an object in IE though .NET?
Posted by sulphi on Sunday, June 14, 2009, 6:45 pm
anvarRank: 228
This code automates control over IE and firefox. Download the code to see how they do it.
Hope it helps
Posted by anvar on Sunday, June 14, 2009, 8:10 pm
xtrmprgrmrRank: 7
Here are some samples from code I've written to control IE, maybe it can help:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
//...
void SetField(WebBrowser wb, string formname, string fieldname, string fieldvalue) {
HtmlElement f = wb.Document.Forms[formname].All[fieldname];
f.SetAttribute("value", fieldvalue);
}
void SetRadio(WebBrowser wb, string formname, string fieldname, bool isChecked) {
HtmlElement f = wb.Document.Forms[formname].All[fieldname];
f.SetAttribute("checked", isChecked ? "True" : "False");
}
void SubmitForm(WebBrowser wb, string formname) {
HtmlElement f = wb.Document.Forms[formname];
f.InvokeMember("submit");
}
void ClickButtonAndWait(WebBrowser wb, string buttonname,int timeOut) {
HtmlElement f = wb.Document.All[buttonname];
webReady = false;
f.InvokeMember("click");
DateTime endTime = DateTime.Now.AddSeconds(timeOut);
bool finished = false;
while (!finished) {
if (webReady)
finished = true;
Application.DoEvents();
if (aborted)
throw new EUserAborted();
Thread.Sleep(50);
if ((timeOut != 0) && (DateTime.Now>endTime)) {
finished = true;
}
}
}
void ClickButtonAndWait(WebBrowser wb, string buttonname) {
ClickButtonAndWait(wb, buttonname, 0);
}
void Navigate(string url,int timeOut) {
webReady = false;
webBrowser1.Navigate(url);
DateTime endTime = DateTime.Now.AddSeconds(timeOut);
bool finished = false;
while (!finished) {
if (webReady)
finished = true;
Application.DoEvents();
if (aborted)
throw new EUserAborted();
Thread.Sleep(50);
if ((timeOut != 0) && (DateTime.Now > endTime)) {
finished = true;
}
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
webReady = true;
}
Posted by xtrmprgrmr on Sunday, June 14, 2009, 10:45 pm
Pages: « Previous1Next »