4

Below is is an HTML form and below that, is a vb procedure, "LoginExamp" that enters in the username and password. I am unable to locate the button however and click it since it does not seem to show up as a mshtml.HTMLInputElement. "htmlInput.click()" never runs. How can I adjust the loginExamp code so that the button is clicked. Thanks for any help.

<form id="loginform" name="loginform" method="post" action="">
<input id="username" class="formfield" type="text" value="User Name" maxlength="40" name="Xusername">
<input id="password" class="formfield" type="password" onfocus="clearDefault(this)" maxlength="40" name="Xpassword">
<button class="subButton" onclick="javascript: submitform()">submit!</button>
</form>

With the below code

Public Sub loginExamp()
    Dim objIE As SHDocVw.InternetExplorer
    Dim htmlDoc As mshtml.HTMLDocument
    Dim htmlInput As mshtml.HTMLInputElement
    Dim htmlColl As mshtml.IHTMLElementCollection
    Dim url As Object
    url = "http://localhost/ButtonClickTest.html" 'just a test page with the loginform above
    objIE = New SHDocVw.InternetExplorer
    With objIE
        .Navigate(url)
        .Visible = True
        While .Busy = True
            Threading.Thread.Sleep(2000)
        End While
        htmlDoc = .Document
        htmlColl = htmlDoc.getElementsByTagName("INPUT")
        While .Busy = True
            Threading.Thread.Sleep(2000)
        End While
        For Each htmlInput In htmlColl
            If htmlInput.name = "Xusername" Then
                htmlInput.value = "theusername" 'this works
            ElseIf htmlInput.name = "Xpassword" Then
                htmlInput.value = "thepassword" 'This works too
            End If
            If htmlInput.className = "subButton" Then 'This is never true
                htmlInput.click() 'This line never runs
            End If
        Next htmlInput
    End With
End Sub
neil860208
  • 167
  • 3
  • 11
  • 1
    I see you changed the class name of your button to subButton. It was different at first; was it always subButton? – bschultz Jan 31 '12 at 22:46
  • Yes it was a mistake in the example code but is not the cause of the problem in fact htmlInput.className is = "nothing" during the for next loop. – neil860208 Jan 31 '12 at 22:48
  • 1
    Instead of using class and className, have you tried using name like you did with the input elements? – bschultz Jan 31 '12 at 22:52
  • In the for each loop only two items are found (two loops), the username and password. So the button does not even seem to exist as part of the mshtml.HTMLInputElement. Also the button does not have a name, just a class. – neil860208 Jan 31 '12 at 22:56
  • 1
    Where did you find SHDocVw? I am using it from Windows\System32 but I keep getting funky errors and I wanted to ensure I'm using the correct DLL. – Peder Rice Nov 29 '12 at 19:26

1 Answers1

3

I was able to finally figure out the answer to my own question. Below is the code that works. Both the username and password is entered and also the button is clicked.

Public Sub loginExamp()
    Dim objIE As SHDocVw.InternetExplorer
    Dim htmlDoc As mshtml.HTMLDocument
    Dim htmlInput As Object
    Dim url As String
    url = "http://localhost/N1/ButtonClickTest.html"'test page with the loginform above
    objIE = New SHDocVw.InternetExplorer
    With objIE
        .Navigate(url)
        .Visible = True
        While .Busy = True
            Threading.Thread.Sleep(2000)
        End While
        htmlDoc = .Document
        Dim htmlColl As mshtml.HTMLFormElement = DirectCast(htmlDoc.forms.item("loginform"), mshtml.HTMLFormElement)
        While .Busy = True
            Threading.Thread.Sleep(2000)
        End While
        For Each htmlInput In htmlColl
            If htmlInput.name = "Xusername" Then
                htmlInput.value = "theusername"
            ElseIf htmlInput.name = "Xpassword" Then
                htmlInput.value = "thepassword"
            End If
            If htmlInput.className = "subButton" Then
                htmlInput.click()
            End If
        Next htmlInput
    End With
End Sub
neil860208
  • 167
  • 3
  • 11
  • 1
    Ugh... per this post http://stackoverflow.com/questions/1014815/how-do-you-get-watin-to-work-on-windows-server-2008-with-ie8 we need to run Visual Studio as an Administrator in order for this to work? How does that when deploying the app? – Peder Rice Nov 29 '12 at 19:31