C# 如何在 WebBrowser 控件内提交表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2299273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I submit a form inside a WebBrowser control?
提问by Mohammad Reza Tavakkol
How can I create a program with C# to submit the form(in the web browser CONTROL in windows Apps)automaticlly ?
如何使用 C# 创建一个程序来自动提交表单(在 Windows 应用程序中的 Web 浏览器 CONTROL 中)?
回答by meagar
The WebBrowser controlhas a Document property, which returns an HtmlDocument. The HtmlDocument has several membersyou can use to traverse and manipulate the DOM.
该web浏览器控件具有文档属性,它返回一个的HTMLDocument。HtmlDocument 有几个成员可以用来遍历和操作 DOM。
Once you've used these methods to find the form, you can use InvokeMemberto call the form's submit method.
使用这些方法查找表单后,您可以使用InvokeMember调用表单的提交方法。
If you know the page has a single form:
如果您知道该页面只有一个表单:
foreach (HtmlElement form in webBrowser1.Document.Forms)
form.InvokeMember("submit");
If you know the ID of the form you would like to submit:
如果您知道要提交的表单的 ID:
HtmlElement form = webBrowser1.Document.GetElementById("FormID");
if (form != null)
form.InvokeMember("submit");
回答by Oliver K?tter
If you know the page has a single form or you want the first form:
如果您知道页面只有一个表单,或者您想要第一个表单:
HTMLDocument doc = webBrowser.Document as HTMLDocument;
HTMLFormElement form = doc.all.OfType<HTMLFormElement>().First();
form.submit();
回答by unol
WebBrowser.Document.GetElementById("form_submit").InvokeMember("click");