Results 1 to 4 of 4

Thread: [2005] a Webpage Manipulation Class for using the AxWebBrowser Control

Threaded View

  1. #1

    Thread Starter
    Lively Member RickyH's Avatar
    Join Date
    Oct 2007
    Posts
    92

    Arrow [2005] a Webpage Manipulation Class for using the AxWebBrowser Control

    I'm trying to build a reusable class for WebPage Manipulation in Vb.NET. I've used Kleinmas great code to base the class I'm posting here. My goal is to show you the class and allow you to build on it so you can simply copy/paste the class into your own project to use. As well, hopefully get some ideas from others on the functions/procedures available in the class.

    If you think I should add a function/procedure, please just post here and I will try to edit the code below to reflect those changes.

    This work was derived from Kleinma's work located in this thread:
    http://www.vbforums.com/showthread.php?t=416275

    You will need to add a few references before using the classes. Here are the steps to add the references:
    Code:
    In your Visual Studio 2005 Solution, we need to add the WebBrowser 
    control to the Toolbox, in Windows Forms, right click and select "Add 
    new item...", and select "COM components" tab. Here select "Microsoft 
    Web Browser". This will add this control to the Toolbox and generate two 
    Interop in your references: AxSHDocVw and SHDocVw. 
    
    You will also need to add a reference to Microsoft.mshtml.  In your 
    vb.net project right-click the references folder and choose to add a 
    new reference. Choose the Microsoft.mshtml and add the reference.
    Here is a couple of examples to use the class to get the source of a webpage and to fill out a simple webform. After adding a AxWebBrowser control and navigating to a page, you can do this: (make sure you rplace the Username, Password, SubmitButton names with their real names on the webpage you are trying to submit to.)
    vb Code:
    1. 'declaration
    2. dim wpM as new clsWebPageManipulation
    3.  
    4. Private Sub Form1_Load(byval yadadada) handles mybase.load
    5.       AxWebBrowser1.Navigate("http://www.vbforums.com")    
    6. Exit Sub
    7.  
    8. Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
    9.      dim strSource as string = wpM.GetSource(AxWebBrowser1)
    10.      console.writeLine(strSource)
    11. End Sub
    12.  
    13. Private Sub FillFormAndClickSubmit
    14.      wpM.SetTextboxText(AxWebBrowser1, "loginform", "RickyH", "txtUserName")
    15.      wpM.SetTextboxText(AxWebBrowser1, "loginform", "myStrongPassword", "txtPassword")
    16.      wpM.ClickSubmitButton(AxWebBrowser1, "loginform", "LoginButtonName")
    17. End Sub

    See attached picture of the current functions available in the class.

    Here is the class that you can store separately in your project:
    vb Code:
    1. Public Class clsWebPageManipulation
    2.  
    3.     Friend Function GetCurrentWebDoc(ByVal wb As AxSHDocVw.AxWebBrowser) As mshtml.HTMLDocument
    4.         Try
    5.             Return DirectCast(wb.Document, mshtml.HTMLDocument)
    6.         Catch ex As Exception
    7.             Return Nothing
    8.         End Try
    9.     End Function
    10.  
    11.     Friend Function GetCurrentWebForm(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal formName As String) As mshtml.HTMLFormElement
    12.         Try
    13.             If GetCurrentWebDoc(wb).forms.length > 0 Then
    14.                 Return DirectCast(GetCurrentWebDoc(wb).forms.item(formName), mshtml.HTMLFormElement)
    15.             Else
    16.                 Return Nothing
    17.             End If
    18.         Catch ex As Exception
    19.             Return Nothing
    20.         End Try
    21.     End Function
    22.  
    23.     Friend Function TextExists(ByVal Text As String, ByVal wb As AxSHDocVw.AxWebBrowser) As Boolean
    24.         Dim mydoc As mshtml.HTMLDocument = GetCurrentWebDoc(wb)
    25.         Dim MyRange As mshtml.IHTMLTxtRange = Nothing
    26.         MyRange = DirectCast(mydoc.selection().createRange, mshtml.IHTMLTxtRange)
    27.         If MyRange.findText(Text) Then
    28.             Return True
    29.         Else
    30.             Return False
    31.         End If
    32.     End Function
    33.  
    34.     Friend Function ShowText(ByVal Text As String, ByVal wb As AxSHDocVw.AxWebBrowser) As Boolean
    35.         Dim mydoc As mshtml.HTMLDocument = GetCurrentWebDoc(wb)
    36.         Dim MyRange As mshtml.IHTMLTxtRange = Nothing
    37.         MyRange = DirectCast(mydoc.selection().createRange, mshtml.IHTMLTxtRange)
    38.         If MyRange.findText(Text) Then
    39.             Return True
    40.         Else
    41.             Return False
    42.         End If
    43.     End Function
    44.  
    45.     Friend Sub SetTextboxText(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal TextToType As String, ByVal txtBoxName As String)
    46.         DirectCast(GetCurrentWebForm(wb, strFormName).item(txtBoxName), mshtml.HTMLInputElement).value = TextToType
    47.     End Sub
    48.  
    49.     'CLICKS THE REGULAR HTML BUTTON (WHICH CALLS A JAVASCRIPT IN THE HTML PAGE TO SHOW THAT IT WORKED)
    50.     Friend Sub ClickNormalButton(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strButtonName As String)
    51.         DirectCast(GetCurrentWebForm(wB, strFormName).item(strButtonName), mshtml.HTMLButtonElement).click()
    52.     End Sub
    53.  
    54.     'THERE ARE 2 WAYS TO SUBMIT A FORM
    55.     Friend Sub ClickSubmitButton(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strButtonName As String)
    56.  
    57.         DirectCast(GetCurrentWebForm(wB, strFormName).item(strButtonName), mshtml.HTMLButtonElement).click()
    58.  
    59.         Return 'so we don't call the below method
    60.  
    61.         'THE OTHER WAY IS HERE, HOWEVER IT WILL BYPASS ANY JAVASCRIPT OR OTHER FUNCTIONALITY
    62.         'THAT MAY BE ATTACHED TO THE BUTTON'S CLICK, WHICH IS WHY THE ABOVE METHOS IS BETTER
    63.         'FOR ALMOST ALL SITUATIONS
    64.         GetCurrentWebForm(wB, strFormName).submit()
    65.     End Sub
    66.  
    67.     Friend Sub ClickImageButton(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strImageButtonName As String)
    68.         Dim MyImageElement As mshtml.HTMLInputElement = _
    69.             DirectCast(GetCurrentWebDoc(wb).all.item(strImageButtonName), mshtml.HTMLInputElement)
    70.         MyImageElement.click()
    71.     End Sub
    72.  
    73.     Friend Function TextSourceExists(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal TextToFind As String) As Boolean
    74.         Try
    75.             TextToFind = TextToFind.ToLower
    76.             If GetCurrentWebDoc(wb).documentElement.outerHTML.ToString.ToLower.Contains(TextToFind) = True Then
    77.                 Return True
    78.             Else
    79.                 Return False
    80.  
    81.             End If
    82.         Catch ex As Exception
    83.             Return False
    84.         End Try
    85.     End Function
    86.  
    87.     Friend Function TextExistsInUrl(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal TextToFind As String) As Boolean
    88.         Try
    89.             If GetCurrentWebDoc(wB).url.ToString.Contains(TextToFind) = True Then
    90.                 Return True
    91.             Else
    92.                 Return False
    93.             End If
    94.         Catch ex As Exception
    95.             Return False
    96.         End Try
    97.     End Function
    98.  
    99.     Friend Function GetSource(ByVal wb As AxSHDocVw.AxWebBrowser) As String
    100.         Try
    101.             Dim str As String = ""
    102.             str = GetCurrentWebDoc(wb).documentElement.outerHTML.ToString
    103.             Return str
    104.         Catch ex As Exception
    105.             Return ""
    106.         End Try
    107.     End Function
    108.  
    109.     Friend Sub SetTextareaText(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextAreaName As String, ByVal strTextToType As String)
    110.         DirectCast(GetCurrentWebForm(wB, strFormName).item(strTextAreaName), mshtml.HTMLTextAreaElement).value = strTextToType
    111.     End Sub
    112.  
    113.     Friend Function GetTextAreaData(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextAreaBoxName As String)
    114.         Try
    115.             If DirectCast(GetCurrentWebForm(wb, strFormName).item(strTextAreaBoxName), mshtml.HTMLTextAreaElement).value <> "" Then
    116.                 Return True
    117.             Else
    118.                 Return False
    119.             End If
    120.         Catch ex As Exception
    121.             Return False
    122.         End Try
    123.     End Function
    124.  
    125.     Friend Function GetTextBoxData(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextBoxName As String)
    126.         Try
    127.             If DirectCast(GetCurrentWebForm(wb, strFormName).item(strTextBoxName), mshtml.HTMLInputElement).value <> "" Then
    128.                 Return True
    129.             Else
    130.                 Return False
    131.             End If
    132.         Catch ex As Exception
    133.             Return False
    134.         End Try
    135.     End Function
    136.  
    137.     Friend Function GetUrl(ByVal wb As AxSHDocVw.AxWebBrowser) As String
    138.         Try
    139.             Return GetCurrentWebDoc(wb).url.ToString
    140.         Catch ex As Exception
    141.             Return ""
    142.         End Try
    143.     End Function
    144.  
    145.     Friend Function GetWebFrameObject(ByVal PassDocumentObject As Object) As mshtml.HTMLWindow2
    146.         Try
    147.             Return DirectCast(PassDocumentObject, mshtml.HTMLWindow2)
    148.         Catch ex As Exception
    149.             Return Nothing
    150.         End Try
    151.     End Function
    152.  
    153.     Friend Function GetWebForm(ByVal objDoc As mshtml.HTMLDocument) As mshtml.HTMLFormElement
    154.         Try
    155.             Return DirectCast(objDoc.forms.item(0), mshtml.HTMLFormElement)
    156.  
    157.         Catch ex As Exception
    158.             Return Nothing
    159.         End Try
    160.     End Function
    161.  
    162.     Friend Sub SetFrameTextBox(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal FrameNumber As Integer, ByVal ValueToSendToBox As String, ByVal FieldToFill As String)
    163.         DirectCast(GetWebForm(GetWebFrameObject(GetCurrentWebDoc(wB).frames.item(FrameNumber)).document).item(FieldToFill, 0), mshtml.HTMLInputElement).value = ValueToSendToBox
    164.     End Sub
    165.  
    166.     Friend Sub clickSubmitButtonFrames(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal FrameNumber As Integer, ByVal FieldToFill As String)
    167.         DirectCast(GetWebForm(GetWebFrameObject(GetCurrentWebDoc(wB).frames.item(FrameNumber)).document).item(FieldToFill, 0), mshtml.HTMLButtonElement).click()
    168.     End Sub
    169.  
    170. End Class
    Attached Images Attached Images  
    Last edited by RickyH; Oct 26th, 2007 at 04:27 PM. Reason: update

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width