Невозможно найти и нажать кнопку отправки, используя mshtml.HTMLInputElement - PullRequest
3 голосов
/ 01 февраля 2012

Ниже приведена HTML-форма, а ниже - процедура vb «LoginExamp», которая вводится в имя пользователя и пароль. Однако я не могу найти кнопку и щелкнуть ее, поскольку она не отображается как mshtml.HTMLInputElement. "htmlInput.click ()" никогда не запускается. Как я могу настроить код loginExamp, чтобы кнопка была нажата. Спасибо за любую помощь.

<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>

С кодом ниже

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

1 Ответ

3 голосов
/ 01 февраля 2012

Мне удалось наконец найти ответ на свой вопрос. Ниже приведен код, который работает. И имя пользователя, и пароль введены, а также нажата кнопка.

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...