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 Bookmark and Share Email
Showing comments 1 to 4 of total 4 on page 1 of 1
Tagged C#, COM, IE
Post reply
« Previous1Next »

sulphi
Rank: 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
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

piyush
Rank: 195

Try this tutorial!

Posted by piyush on Sunday, June 14, 2009, 7:12 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

anvar
Rank: 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
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

xtrmprgrmr
Rank: 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
  • Currently 0.00/5

0 votes

Thank this userFlag this comment
Pages: « Previous1Next »

Post your comment (No registration required)

  Add my comment  

TechieDesi Community

Not signed in (Sign-in or Register)
Be a true TechieDesi!
Top 10 Users
Spread the word
Invite your friends
Fan stuff
Help us improve
Need Help
FAQ's
Search tips
Found a bug? Report!
Feeds and letters
Subscribe via RSS
Archives
Subscribe to newsletter
Unsubscribe e-mail
Miscellaneous
Privacy policy
Visit rootnerve
About us
About us
Support the development
Official Blog
Advertise with us
Careers
Copyright (c) 2008, TechieDesi.com. All rights reserved | About us | Do-Not-Disturb registry | Powered by rootnerve | Page rendered in 0.105 seconds