Exploring the depths and potentials of ASP.NET RSS 2.0 or Subscribe to .BenRush by Email
 Wednesday, April 04, 2007

Say you want to use the browser component to automate some form of web browser experience - for example, say you want to programmatically enter some value into a textbox and click a button using C# or VB.Net. To do this is actually quite simple (and, dare we say it, fun).

First, you simply add your browser component to your windows form.

Second, you register an event handler with the browser component for the DocumentCompleted event:

            this.webBrowser1.Url = new Uri("http://www.google.com");
            this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

Third, you fill in the event handler with something that looks like this:

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!_clicked)
            {
                _clicked = true;

                HtmlElement queryBox = this.webBrowser1.Document.All["q"];
                queryBox.SetAttribute("value", "hello world");

                HtmlElement goButton = this.webBrowser1.Document.All["btnG"];
                goButton.InvokeMember("click");
            }

            return;
        }

This will load the www.google.com homepage, fill in their submit text box and click the Search button.

In the golden days of MSHTML component reuse (before the niceness of .Net'ifying the browser DOM) you had to use a RCW (Runtime Callable Wrapper) around the MSHTML COM library. Everything is obviously implemented through IDispatch here (because you have to be able to do this stuff from VB6 or scripting environments), and so much of the invocation of methods (like click) was done by loosly picking the name for the method and building the stack frame through arrays, etc. The RCW made things a bit nicer for you (you didn't have to think about IDispatch), but you still had to think about COM.

It's been years since I've developed in COM and ATL and I'm glad I don't have to anymore.

Recommended reading:


kick it on DotNetKicks.com
Wednesday, April 04, 2007 10:55:24 AM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
Programming | WinForms and WPF
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview

Computers Blogs - Blog Top Sites

Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Benjamin Rush
Sign In
Statistics
Total Posts: 444
This Year: 0
This Month: 0
This Week: 0
Comments: 127
Themes
Pick a theme:
All Content © 2008, Benjamin Rush
DasBlog theme 'Business' created by Christoph De Baene (delarou)