使用html元素在Web浏览器上提交按钮

时间:2013-04-18 04:33:38

标签: html vb.net web

好的,所以这个网站http://dogwars.com/app/

我正在创建一个可以登录的东西。

但是网站上的登录按钮:

<div style="margin-left: 77px; margin-top: 20px; height: 40px;">
   <a href="#" onclick="document.getElementById('loginForm').submit();" class="button"><span>Login</span></a>
  </div>

我在VB.net上的自动提交按钮我用这个:

If curElement.GetAttribute("onclick").Equals("document.getElementById('loginForm').submit();") Then
            curElement.InvokeMember("click")
        End If

但是它不会自动登录而且我很困惑为什么它不会..?
有人可以帮我吗? 这是我的完整代码:

Public Class Form1

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    WebBrowser1.Navigate("http://dogwars.com/app/")

End Sub

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

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

    For Each curElement As HtmlElement In theElementCollection

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

        If controlName = "email" Then

            curElement.SetAttribute("Value", TextBox1.Text)

        ElseIf controlName = "password" Then

            curElement.SetAttribute("Value", TextBox2.Text)

        End If

    Next


    theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
    For Each curElement As HtmlElement In theElementCollection
        If curElement.GetAttribute("onclick").Equals("document.getElementById('loginForm').submit();") Then
            curElement.InvokeMember("click")
        End If
    Next
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您过度使用HTML Elements复杂化了。这是你应该做的:

            WebBrowser1.Navigate("http://dogwars.com/app/")

            Do Until WebBrowser1.ReadyState = WebBrowserReadyState.Complete
                Application.DoEvents()
            Loop

            WebBrowser1.Document.GetElementById("email").SetAttribute("value",TextBox1.Text())
            WebBrowser1.Document.GetElementById("password").SetAttribute("value",TextBox2.Text())
            WebBrowser1.Document.GetElementById("submit").InvokeMember("click")

这应该可行但我还没有测试过。