brunoaduarte
June 8th, 2009, 05:44 AM
I'm trying to fill a web ASPX form thru a VB.NET desktop program using the IE control SHDocVw.InternetExplorer
So far i can set all the textfields that i have to set and click the send button, but i have a small problem: i have to execute a "validation" java script code that is on the ASPX page before clicking the SUBMIT button.
Here's my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wbbrowser As New SHDocVw.InternetExplorer
For Each wbbrowser In New ShellWindows
If wbbrowser.LocationURL = "http://my.website.com/test.aspx" Then
FillForm(wbbrowser)
Exit Sub
End If
Next
Do
Loop Until Not wbbrowser.Busy
End Sub
Public Sub FillForm(ByRef wbBrowser As SHDocVw.InternetExplorer)
Dim HTMLDoc As mshtml.HTMLDocument
Dim iHTMLCol As IHTMLElementCollection
Dim iHTMLEle As IHTMLElement
Dim str As String
Do
Loop Until Not wbBrowser.Busy
HTMLDoc = wbBrowser.Document
'HERE I GET THE FRAME WHERE THE FORM IS
Dim F9 As mshtml.IHTMLWindow2 = CType(HTMLDoc.frames.item(9), IHTMLWindow2)
Dim FrmOrdem As mshtml.HTMLDocument = CType(F9.document, mshtml.HTMLDocument)
'GET THE ELEMENTS FROM THE FORM FRAME
iHTMLCol = FrmOrdem.getElementsByTagName("input")
'CLICK BUTTON TO CLEAN FORM
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "btnLimpar" Then
iHTMLEle.click()
Exit For
End If
End If
Next
'SET FORM TEXT FIELD
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "txtBoxPapel" Then
iHTMLEle.setAttribute("value", TextBox1.Text)
'iHTMLEle.Blur() 'I TRIED THIS TO EXECUTE THE OnBlur action from this textbox
'iHTMLEle.Focus() 'I TRIED THIS TO EXECUTE THE OnBlur action from this textbox
Exit For
End If
End If
Next
'PRESS THE SUBMIT BUTTON
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "btnIncluir" Then
iHTMLEle.click()
Exit For
End If
End If
Next
Do
Loop Until Not wbBrowser.Busy
End Sub
End Class
On the ASPX page the code for the txtBoxPapel is:
<input name="txtBoxPapel" type="text" maxlength="8" id="txtBoxPapel" title="Papel" class="Input" OnKeyUp="ToUpper(this);" OnBlur="OnBlurPapel(this);" style="width: 60px;" />
What i need is to execute this OnBlur="OnBlurPapel(this);" right after setting it's value and before clicking the SUBMIT button, so it executes this OnBlurPapel(this) function and validate the typed data before sending it.
Please help, thanks
So far i can set all the textfields that i have to set and click the send button, but i have a small problem: i have to execute a "validation" java script code that is on the ASPX page before clicking the SUBMIT button.
Here's my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wbbrowser As New SHDocVw.InternetExplorer
For Each wbbrowser In New ShellWindows
If wbbrowser.LocationURL = "http://my.website.com/test.aspx" Then
FillForm(wbbrowser)
Exit Sub
End If
Next
Do
Loop Until Not wbbrowser.Busy
End Sub
Public Sub FillForm(ByRef wbBrowser As SHDocVw.InternetExplorer)
Dim HTMLDoc As mshtml.HTMLDocument
Dim iHTMLCol As IHTMLElementCollection
Dim iHTMLEle As IHTMLElement
Dim str As String
Do
Loop Until Not wbBrowser.Busy
HTMLDoc = wbBrowser.Document
'HERE I GET THE FRAME WHERE THE FORM IS
Dim F9 As mshtml.IHTMLWindow2 = CType(HTMLDoc.frames.item(9), IHTMLWindow2)
Dim FrmOrdem As mshtml.HTMLDocument = CType(F9.document, mshtml.HTMLDocument)
'GET THE ELEMENTS FROM THE FORM FRAME
iHTMLCol = FrmOrdem.getElementsByTagName("input")
'CLICK BUTTON TO CLEAN FORM
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "btnLimpar" Then
iHTMLEle.click()
Exit For
End If
End If
Next
'SET FORM TEXT FIELD
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "txtBoxPapel" Then
iHTMLEle.setAttribute("value", TextBox1.Text)
'iHTMLEle.Blur() 'I TRIED THIS TO EXECUTE THE OnBlur action from this textbox
'iHTMLEle.Focus() 'I TRIED THIS TO EXECUTE THE OnBlur action from this textbox
Exit For
End If
End If
Next
'PRESS THE SUBMIT BUTTON
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "btnIncluir" Then
iHTMLEle.click()
Exit For
End If
End If
Next
Do
Loop Until Not wbBrowser.Busy
End Sub
End Class
On the ASPX page the code for the txtBoxPapel is:
<input name="txtBoxPapel" type="text" maxlength="8" id="txtBoxPapel" title="Papel" class="Input" OnKeyUp="ToUpper(this);" OnBlur="OnBlurPapel(this);" style="width: 60px;" />
What i need is to execute this OnBlur="OnBlurPapel(this);" right after setting it's value and before clicking the SUBMIT button, so it executes this OnBlurPapel(this) function and validate the typed data before sending it.
Please help, thanks