Search
Enter Keywords:
Home
Web Browsing and Form Completion with Visual Basic 6 - Part 2 PDF Print E-mail
User Rating: / 5
PoorBest 
Written by Michael Salsbury   
Wednesday, 22 March 2006

In the last installment of this series, we looked at how you gain access to the HTML code of a web page using the Visual Basic 6 WebBrowser object.   That's a good first step toward doing something a bit more interesting, which is completing a web form using the VB6 WebBrowser object.

Last time around we wrote a simple program that took a look at my home page and showed you the HTML. Now, we're going to use the information we learned there to figure out how to automatically search my site for anything mentioning Visual Basic.

The first step is to identify the form used on my site for the search function. As it turns out, the search form on my site is contained in the following code:

<FORM action=index.php method=post>
<DIV class=searchblock id=searchblock>Enter Keywords: <INPUT 
      class=inputbox onblur="if(this.value=='') this.value='search...';" 
      style="WIDTH: 128px" onfocus="if(this.value=='search...') 
      this.value='';" size=15 value=search... name=searchword> <INPUT 
      type=hidden value=search name=option
<DIV align=left><INPUT class=button style="WIDTH: 35px" type=submit 
      value=GO> </DIV></DIV>
</FORM>

Since this is the first form in this particular web document, we should be able to access it as "Forms(0)" through the WebBrowser.Document object's "Forms()" collection. (If it had been the second form to appear on the page, we'd use "Forms(1)", etc.)   To see if this is the case, we add the following code to the WebBrowser1_DocumentComplete event to read as follows:

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL 
      As Variant)
   If URL <> txtURL.Text Then Exit Sub   
If pDisp <> WebBrowser1.Object Then Exit Sub
txtHTMLDisplay.Text = WebBrowser1.Document.Body.innerhtml
txtURL.Text = WebBrowser1.LocationURL
WebBrowser1.Document.Forms(0).Item(0).Value = "Visual Basic"
End Sub

When we run the program this time, sure enough the program fills the web form's search term box with the phrase "Visual Basic":

Search Form Filled In
The Search Form, Filled In

 

Now we need to have the program submit that form. Looking at the HTML code above for the form, we see that the submit button is the THIRD Item in the Form (since counting starts from zero, the third item will be "Item(2)"). To have the program automatically click that button for us and start the search, we add the following line to the end of the DocumentComplete subroutine's code:

   WebBrowser1.Document.Forms(0).Item(2).Click

After running it again, we see that the program loads my home page, fills in the search term "Visual Basic"  and clicks the submit button to submit the search, obtaining some search results:

Search Results Returned
Search Results Returned

This was by design a very simple example. We'll look at a much more complicated form next time and see how we would go about automating it using Visual Basic.


Related Blogs:

Related Links:

Last Updated ( Friday, 31 March 2006 )
< Previous   Next >

Main Menu
Home
Blog
Photos
Links
Search
Site Index
Feedback
Administrator
Featured Links
BlogInspiration
SpamToons
Shawn Prince's Blog
Jack Ludwig's Blog
Mike Cramer's Site
Fark
Slashdot
Woot!
Cigar Envy
John Kricfalusi's Blog
CigarBlog 101
Cigars 101 Forum
Sponsored Links


View Site Stats