自动点击C#ASP.NET应用程序中的Web页面按钮

时间:2015-03-24 16:23:49

标签: c# asp.net mouseevent

我知道这个问题已在这里得到解决,但是我无法获得任何示例代码。基本上,我想在C#ASP.NET应用程序中模拟单击Web页面上的按钮。我的代码连接到网页但不调用点击事件。

这是我的代码:

WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate("URL to navigate to");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) System.Windows.Forms.Application.DoEvents();
webBrowser1.Document.GetElementById("agree_terms_use").InvokeMember("click");

HTML:

<button id=agree_terms_use name="agreementValue" type="submit" value="Agree">Agree</button>        

1 个答案:

答案 0 :(得分:0)

我知道现在有点老了,但我遇到了同样的问题(假设这与移动的Feds新电子支付路由目录有关,现在有一个“使用条款/协议”页面)。关闭您的初始代码我能够得到以下工作并创建文件

//Load page that will end up as the "Agreement" page
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate(url);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
    System.Windows.Forms.Application.DoEvents();

//submit agree button click
webBrowser1.Document.GetElementById("agree_terms_use").InvokeMember("Click");
webBrowser1.Document.Forms[1].InvokeMember("submit");

//Navigate back to the page which should now allow access to the data
WebBrowser webBrowser2 = new WebBrowser();
webBrowser2.Navigate(url);
while (webBrowser2.ReadyState != WebBrowserReadyState.Complete)
    System.Windows.Forms.Application.DoEvents();

HtmlElement body = webBrowser2.Document.Body;
string webResults = body.InnerHtml;
webResults = webResults.Replace("<PRE>", "").Replace("</PRE>", "");
webResults = webResults.Replace("&amp;", "&");

StreamWriter file = File.CreateText(outputFile);
file.Write(webResults);
file.Close();