1

我必须以编程方式单击网站第三页上的 HTML 按钮。该按钮没有ID。它只有名称 type 和 value 。该按钮的 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 平台。谁能告诉我点击它的解决方案?

4

5 回答 5

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
于 2012-06-19T12:46:04.940 回答
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

测试和工作!

于 2013-07-11T14:03:04.797 回答
1

尝试在表单上调用提交,而不是单击输入。

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

Dim l_forms = WebBrowser1.Document.GetElementsByTagName("form")
If l_forms.Count > 0 Then
  l_forms.Item(0).InvokeMember("submit")
End If
于 2012-06-20T03:52:21.767 回答
0

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

因此,要么使用 Javascript,要么document.form1.submit()调用连接到按钮的 VB 子程序btnsub.click(或您命名的任何名称)。

于 2012-06-19T12:45:46.033 回答
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

;)

于 2012-06-20T11:22:44.150 回答