如何通过vb.net以编程方式单击HTML按钮

时间:2012-06-23 04:23:08

标签: vb.net

我正在尝试以编程方式单击按钮。该按钮位于网站的第3页。它没有任何身份证明。它只有名称,类型和价值。该按钮的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> 

我在vb.net 2008快递版中试过这些代码......

For Each webpageelement As HtmlElement In allelements

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

                webpageelement.InvokeMember("click")

            End If
        Next

第二个

theElementCollection = WebBrowser1.Document.GetElementsByTagName("INPUT")

    For Each curElement As HtmlElement In theElementCollection

        ctrlIdentity = curElement.GetAttribute("innerText").ToString

        If ctrlIdentity = "Next" Then

            curElement.InvokeMember("click")

        End If

    Next

和第三个是

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

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

,最后一个是

Dim i As Integer
        Dim allButtons As HtmlElementCollection

        allButtons = WebBrowser1.Document.GetElementsByTagName("input")

        i = 0
        For Each webpageelement As HtmlElement In allButtons
            i += 1
            If i = 5 Then
                webpageelement.InvokeMember("click")
            End If
        Next

这些所有代码都无法点击该按钮。请点击此按钮建议适当的解决方案。非常感谢你

1 个答案:

答案 0 :(得分:0)

类似这样的服务器端JavaScript和jquery解决方案

string script = "$(document).ready(function() {$('input[name=btnSub]').trigger('click'); });";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);