以编程方式单击vb.net的html按钮

时间:2012-06-19 12:35:47

标签: vb.net

我必须以编程方式单击HTML按钮,该按钮位于网站的第3页。该按钮没有ID。它只有名称类型和价值。该按钮的HTML代码如下所示

<FORM NAME='form1' METHOD='post' action='/dflogin.php'>
  <INPUT TYPE='hidden' NAME='txtId' value='E712050-15'>
  <INPUT TYPE='hidden' NAME='txtassId' value='1'><INPUT TYPE='hidden' NAME='txtPsw'  value='HH29'>
  <INPUT TYPE='hidden' NAME='txtLog' value='0'><h6 align='right'>
  <INPUT TYPE='SUBMIT' NAME='btnSub' value='Next' style='background-color:#009900; color:#fff;'></h6>
</FORM>

我使用以下代码点击它

For Each webpageelement As HtmlElement In allButtons

        If webpageelement.GetAttribute("value") =  "Start" Then

            webpageelement.InvokeMember("click")

        End If

Next

但我无法点击它。我正在使用vb.net 2008平台。谁能告诉我点击它的解决方案?

5 个答案:

答案 0 :(得分:1)

Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("input")
For Each elem As HtmlElement In Elems
    elem.InvokeMember("click")
Next

答案 1 :(得分:1)

尝试在表单上调用Submit,而不是单击Input。

编辑:糟糕,HTMLElementCollection没有实现通用的IEnumerable。试试这个:

Dim l_forms = WebBrowser1.Document.GetElementsByTagName("form")
If l_forms.Count > 0 Then
  l_forms.Item(0).InvokeMember("submit")
End If

答案 2 :(得分:1)

我花了很长时间试图找到答案。我从未意识到你可以使用javascript调用点击。一旦我读到解决方案变得非常容易:

Public Function clickbyid(ByVal id)

    If TheBrowser.Document.GetElementById(id) IsNot Nothing Then
        Dim Headers As String = "" 'insert headers if you want to

        TheBrowser.Navigate("javascript:document.getElementById('" & id & "').click();", "_self", Nothing, Headers)

        'This keeps the function active until the browser has finished loading
        Do While TheBrowser.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop
        Return 1
    Else

        MessageBox.Show("Could not find link by id" & id)
        Return Nothing
    End If

End Function

您可能需要将“TheBrowser”更改为“WebBrowser1”。 如果您不想返回结果,则只需将其更改为public sub并删除return语句。

如果htmlelement没有id ,请使用以下函数添加一个

Public Function clickbyelement(ByVal theButton As HtmlElement)

    Try

        'Generate a unique id to identify the element
        Dim randomID = "vbAdded" & GetRandom(10000, 100000).ToString
        'check to make sure the ID does not already exist
        While TheBrowser.Document.GetElementById(randomID) IsNot Nothing
            randomID = "vbAdded" & GetRandom(10000, 100000).ToString
        End While
        'add the ID to the element
        theButton.SetAttribute("id", randomID)
        'click
        clickbyid(randomID)
        Return True

    Catch ex As Exception

        Return False

    End Try

End Function

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
    ' by making Generator static, we preserve the same instance '
    ' (i.e., do not create new instances with the same seed over and over) '
    ' between calls '
    Static Generator As System.Random = New System.Random()
    Return Generator.Next(Min, Max)
End Function

感谢Dan Tao的随机数:https://stackoverflow.com/a/2677819/381273

经过测试和工作!

答案 3 :(得分:0)

听起来您正在尝试执行客户端操作 - 在服务器上操作时,单击按钮(VB代码)。这不起作用。

所以,要么使用Javascript document.form1.submit(),要么调用连接到按钮btnsub.click的VB子(或任何你命名的)。

答案 4 :(得分:0)

解决方案:

WebBrowser1.Navigate("UrlHere!")

'wait till the page is laoded

Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop

WebBrowser1.Document.Body.InnerHtml = Replace(WebBrowser1.Document.Body.InnerHtml, "NAME='btnSub'", "NAME='btnSub' id='btnSub'") 'insert id into youre button
WebBrowser1.Document.GetElementById("btnSub").InvokeMember("click") 'click on btnSub