I figured I’d give out a quick tip that I had to recently do in C#. A friend had asked me how to automatically fill out a web form and submit it so that he could grab the html of the results page. Luckily, it is VERY simple in C# and I assume it is just as easy in VB.NET too…
HtmlDocument document = browser.Document;
HtmlElement inputValue = document.GetElementById("ctl00_ContentPlaceHolder1_txtNAICS");
HtmlElement submitButton = document.GetElementById("ctl00_ContentPlaceHolder1_btnSearch2");
What you do is basically grab your HtmlDocument from the browser control AFTER the page has been fully loaded. From here you have some choices, if you know the ID of the elements you want and if it is not a lot of elements you can grab each element and store them into separate HtmlElement objects. If you want to fill out ALL input elements then you can use GetElementsByTagName(“input”) in place of GetElementByID(). The tag name method returns an HtmlElementCollection which you can just throw that line into a foreach loop for every HtmlElement.
Once you have your HtmlElement object that contains the correct input which you want to fill in, you can then call SetAttribute(“value”, “some value”). All you would need to do is change “some value” to whatever value you’re filling in.
Later on, you’ll need to submit the form. I’m not sure if there are various ways of going about this but I found that just by finding the submit element by its ID and invoking “click” seems to work just fine.
I realize this is not very detailed but as I write more C# examples and such I’ll get into a more detailed practice.