【问题标题】:How to simulate Web page button click in code如何在代码中模拟网页按钮点击
【发布时间】:2013-01-16 17:57:59
【问题描述】:

我正在尝试在我的程序中模拟按钮单击,以便从服务器获取响应。问题是网页“onclick”激活了该页面中的脚本

html 看起来像这样:

<script language=javascript>
    function frmsubmit(id)
    {
        document.all("HistoryData1_hiddenID").value=id;
        document.all("Form1").submit();  
    }
</script>
<input type="button" id="btnGo" value="Go" Class="RegularButton" onclick="frmsubmit('0')" >

我使用了我见过的这段代码:

WebBrowser wb = new WebBrowser();
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
    Application.DoEvents();
}
wb.Document.GetElementById("btnGo").InvokeMember("click");

什么都没有发生。在真实的网页中,我得到一个包含我需要的数据的 html。有什么想法吗?

【问题讨论】:

  • HtmlElement btn = yourBrowserControl.Document.GetElementById("btnGo"); btn.InvokeMember("click");

标签: c# .net button onclick simulate


【解决方案1】:

你可以像 jquery 一样使用

$("#btnGo").click();

在您的 .cs 中,您会像使用它一样

StringBuilder sb = new StringBuilder();
sb.AppendLine("$(document).ready(function() {");
sb.AppendLine("$('#btnGo').click();");
sb.AppendLine(" });");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", sb.ToString(), true);

就是这样,当然要在html页面中添加jquery库

【讨论】:

    【解决方案2】:

    尝试调用 doPostBack

    __doPostBack("btnGo', '<event argument here>');
    

    http://wiki.asp.net/page.aspx/1082/dopostback-function/

    【讨论】:

      【解决方案3】:

      使用 jQuery trigger() 函数(或 triggerHandler,取决于您的目的)。 即

      $('#<%=btnGo.ClientID%>').trigger('click');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 1970-01-01
        • 2012-01-28
        • 2021-07-06
        • 2015-06-28
        相关资源
        最近更新 更多