简体   繁体   English

如何模拟网页按钮单击代码

[英]How to simulate Web page button click in code

I'm trying to simulate a button click in my program in order to get the response from the server. 我试图模拟程序中的按钮单击,以便从服务器获得响应。 The problem is that the web page "onclick" activate a script in that page 问题是网页“ onclick”激活该页面中的脚本

The html look like this: 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')" >

I used this code that i have seen: 我使用了我看到的这段代码:

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

Nothing happens. 什么都没发生。 In the real web page I get an html with data that I need. 在真实的网页中,我得到了一个包含所需数据的html。 Any ideas? 有任何想法吗?

you can use jquery like 你可以像这样使用jQuery

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

in your .cs you will use it like 在您的.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);

thats it, of course you have to add jquery libraries to html page 就是这样,当然您必须将jQuery库添加到html页面

Try calling doPostBack 尝试致电doPostBack

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

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

Use jQuery trigger() function (or triggerHandler , dependingo on your purpose). 使用jQuery trigger()函数(或triggerHandler ,取决于您的目的)。 Ie

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM