Question post to a web form in Internet explorer

newbiethicky

Member
Joined
Sep 28, 2011
Messages
7
Programming Experience
1-3
I am trying to programmatically post to a textbox on a web form in IE - I have tried sendkeys - but I dont find them reliable or consistent - The code below is perfect - but only seems to work in a webbrowser control. How do I (click a button/select a radiobutton) send 'hello world' to a textbox on a webform in IE? By the way I would be happy to use the webbrowser control (so the code below would work) - but the webpage (which is not the one in the code below) runs locally on my machine (I dont have any control over it) and does not seem to work within a webbrowser control
Can anyone help??
PS go easy on me - I'm a Newbie/Thick
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Load particular page in WebBrowser object
WebBrowser1.Navigate("http://www.24directory.com.ar/submit.php")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Beep()

' Part 1: Locate the Radio button "Regular links" and automatically click it

'<input type="radio" name="LINK_TYPE" value="normal">

Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")

For Each curElement As HtmlElement In theElementCollection

Dim controlValue As String = curElement.GetAttribute("Value").ToString

If controlValue = "reciprocal" Then

curElement.InvokeMember("click")

End If

Next



' Part 2: Locate the Description RichTextBox and automatically input some text

'<textarea name="DESCRIPTION" rows="3" cols="37" class="text"></textarea>

theElementCollection = WebBrowser1.Document.GetElementsByTagName("textarea")

For Each curElement As HtmlElement In theElementCollection

Dim controlName As String = curElement.GetAttribute("name").ToString

If controlName = "DESCRIPTION" Then

curElement.SetAttribute("Value", "Some description")

End If

Next



' Part 3: Locate the Category ComboBox element and automatically choose one option

'<select name="CATEGORY_ID">

'<option label="[Top]" value="0" selected="selected">[Top]</option>

'<option label="|___Automotive" value="1">|___Automotive</option>

'<option label="|   |___Alarms - Audio - Video" value="734">|   |___Alarms - Audio - Video</option>

'</select>

theElementCollection = WebBrowser1.Document.GetElementsByTagName("select")

For Each curElement As HtmlElement In theElementCollection

Dim controlName As String = curElement.GetAttribute("name").ToString

If controlName = "CATEGORY_ID" Then

curElement.SetAttribute("Value", "10")

End If

Next

End Sub



End Class
 
Had a bit of progress with my project - hope this helps someone -
Imports mshtml
Public Class Form1
Dim IE As SHDocVw.InternetExplorer
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim wbBrowser As New SHDocVw.InternetExplorer
wbBrowser.Visible = True
wbBrowser.Navigate("practisevoodoo", Nothing, Nothing, Nothing, Nothing)
Do
Loop Until Not wbBrowser.Busy
LoginIntoSite(wbBrowser)
OpenComposePage(wbBrowser)
End Sub
Public Sub LoginIntoSite(ByRef wbBrowser As SHDocVw.InternetExplorer)
Dim HTMLDoc As mshtml.HTMLDocument
Do
Loop Until Not wbBrowser.Busy
HTMLDoc = wbBrowser.Document
Dim iHTMLCol As IHTMLElementCollection
Dim iHTMLEle As IHTMLElement
Dim str, userName, passwd As String
iHTMLCol = HTMLDoc.getElementsByTagName("input")
' Type the user name in the username text box
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "UserNameControl" Then
iHTMLEle.setAttribute("value", "MyUserName")
Exit For
End If
End If
Next
' Type the password in the password text box
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "PasswordControl" Then
iHTMLEle.setAttribute("value", "MyPassword")
Exit For
End If
End If
Next

For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "LoginButtonButton" Then
iHTMLEle.click()
Exit For
End If
End If
Next
Do
Loop Until Not wbBrowser.Busy
End Sub
 
Back
Top