Saturday, May 31, 2014

Automating WebBrowser with C# and Open Twebst

The .Net WebBrowser class is actually an Internet Explorer control. You can add it to your Windows Forms application do display HTML content.

Using WebBrowser class from C# code you can have access to HTML document but automating the IE control is not as easy as it should be. You will have to take into account things like:
  • browser events and timeouts: navigating, document complete
  • an asynchronous programming model: web automation code mixed with event handlers
  • document ready state
  • HTML events - you'll have to fire the right events so web pages will react properly
  • dealing with DOM and cross frames security restrictions
  • manually writing web automation code
Open Twebst addresses all this issues with a consistent programming model and an integrated web recorder that assists you in generating web automation code. Basically, things work like this with Open Twebst:
  • attach to WebBrowser control so you can use Twebst objects
  • optionally you can register to OnCancel event so you can cancel web automation at any point (the current web automation method will throw an error when canceled)
  • generate web automation code with Open Twebst web recorder
  • use the code in your C#/VB.Net Windows Forms project
Web automation code executes on UI thread but Open Twebst methods dispatch windows messages under the hood so the application does not freeze. OnCancel event is raised from time to time so you have a chance to stop automation code at any point.

Here is a sample that comes with Open Twebst setup and you can also download from here:

No comments: