Public Class clsWebPageManipulation
Friend Function GetCurrentWebDoc(ByVal wb As AxSHDocVw.AxWebBrowser) As mshtml.HTMLDocument
Try
Return DirectCast(wb.Document, mshtml.HTMLDocument)
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Function GetCurrentWebForm(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal formName As String) As mshtml.HTMLFormElement
Try
If GetCurrentWebDoc(wb).forms.length > 0 Then
Return DirectCast(GetCurrentWebDoc(wb).forms.item(formName), mshtml.HTMLFormElement)
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Function TextExists(ByVal Text As String, ByVal wb As AxSHDocVw.AxWebBrowser) As Boolean
Dim mydoc As mshtml.HTMLDocument = GetCurrentWebDoc(wb)
Dim MyRange As mshtml.IHTMLTxtRange = Nothing
MyRange = DirectCast(mydoc.selection().createRange, mshtml.IHTMLTxtRange)
If MyRange.findText(Text) Then
Return True
Else
Return False
End If
End Function
Friend Function ShowText(ByVal Text As String, ByVal wb As AxSHDocVw.AxWebBrowser) As Boolean
Dim mydoc As mshtml.HTMLDocument = GetCurrentWebDoc(wb)
Dim MyRange As mshtml.IHTMLTxtRange = Nothing
MyRange = DirectCast(mydoc.selection().createRange, mshtml.IHTMLTxtRange)
If MyRange.findText(Text) Then
Return True
Else
Return False
End If
End Function
Friend Sub SetTextboxText(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal TextToType As String, ByVal txtBoxName As String)
DirectCast(GetCurrentWebForm(wb, strFormName).item(txtBoxName), mshtml.HTMLInputElement).value = TextToType
End Sub
'CLICKS THE REGULAR HTML BUTTON (WHICH CALLS A JAVASCRIPT IN THE HTML PAGE TO SHOW THAT IT WORKED)
Friend Sub ClickNormalButton(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strButtonName As String)
DirectCast(GetCurrentWebForm(wB, strFormName).item(strButtonName), mshtml.HTMLButtonElement).click()
End Sub
'THERE ARE 2 WAYS TO SUBMIT A FORM
Friend Sub ClickSubmitButton(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strButtonName As String)
DirectCast(GetCurrentWebForm(wB, strFormName).item(strButtonName), mshtml.HTMLButtonElement).click()
Return 'so we don't call the below method
'THE OTHER WAY IS HERE, HOWEVER IT WILL BYPASS ANY JAVASCRIPT OR OTHER FUNCTIONALITY
'THAT MAY BE ATTACHED TO THE BUTTON'S CLICK, WHICH IS WHY THE ABOVE METHOS IS BETTER
'FOR ALMOST ALL SITUATIONS
GetCurrentWebForm(wB, strFormName).submit()
End Sub
Friend Sub ClickImageButton(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strImageButtonName As String)
Dim MyImageElement As mshtml.HTMLInputElement = _
DirectCast(GetCurrentWebDoc(wb).all.item(strImageButtonName), mshtml.HTMLInputElement)
MyImageElement.click()
End Sub
Friend Function TextSourceExists(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal TextToFind As String) As Boolean
Try
TextToFind = TextToFind.ToLower
If GetCurrentWebDoc(wb).documentElement.outerHTML.ToString.ToLower.Contains(TextToFind) = True Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Friend Function TextExistsInUrl(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal TextToFind As String) As Boolean
Try
If GetCurrentWebDoc(wB).url.ToString.Contains(TextToFind) = True Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Friend Function GetSource(ByVal wb As AxSHDocVw.AxWebBrowser) As String
Try
Dim str As String = ""
str = GetCurrentWebDoc(wb).documentElement.outerHTML.ToString
Return str
Catch ex As Exception
Return ""
End Try
End Function
Friend Sub SetTextareaText(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextAreaName As String, ByVal strTextToType As String)
DirectCast(GetCurrentWebForm(wB, strFormName).item(strTextAreaName), mshtml.HTMLTextAreaElement).value = strTextToType
End Sub
Friend Function GetTextAreaData(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextAreaBoxName As String)
Try
If DirectCast(GetCurrentWebForm(wb, strFormName).item(strTextAreaBoxName), mshtml.HTMLTextAreaElement).value <> "" Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Friend Function GetTextBoxData(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextBoxName As String)
Try
If DirectCast(GetCurrentWebForm(wb, strFormName).item(strTextBoxName), mshtml.HTMLInputElement).value <> "" Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Friend Function GetUrl(ByVal wb As AxSHDocVw.AxWebBrowser) As String
Try
Return GetCurrentWebDoc(wb).url.ToString
Catch ex As Exception
Return ""
End Try
End Function
Friend Function GetWebFrameObject(ByVal PassDocumentObject As Object) As mshtml.HTMLWindow2
Try
Return DirectCast(PassDocumentObject, mshtml.HTMLWindow2)
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Function GetWebForm(ByVal objDoc As mshtml.HTMLDocument) As mshtml.HTMLFormElement
Try
Return DirectCast(objDoc.forms.item(0), mshtml.HTMLFormElement)
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Sub SetFrameTextBox(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal FrameNumber As Integer, ByVal ValueToSendToBox As String, ByVal FieldToFill As String)
DirectCast(GetWebForm(GetWebFrameObject(GetCurrentWebDoc(wB).frames.item(FrameNumber)).document).item(FieldToFill, 0), mshtml.HTMLInputElement).value = ValueToSendToBox
End Sub
Friend Sub clickSubmitButtonFrames(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal FrameNumber As Integer, ByVal FieldToFill As String)
DirectCast(GetWebForm(GetWebFrameObject(GetCurrentWebDoc(wB).frames.item(FrameNumber)).document).item(FieldToFill, 0), mshtml.HTMLButtonElement).click()
End Sub
End Class