MSHTML - 自动单击 JavaScript 确认对话框
我正在尝试使用 MSHTML 自动解析/提交网页(在 C#.Net 3.1 WPF WebBrowser 控件中)。 我可以毫无问题地填写表格、单击按钮和浏览页面。
但我不知道如何在单击“提交”按钮时出现的 JavaScript 确认对话框上自动单击“确定”按钮。
C# 代码:
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser.Document;
mshtml.IHTMLFormElement form = doc.forms.item("inputForm", 0) as mshtml.IHTMLFormElement;
mshtml.IHTMLElement btnSubmit = form.item("btnFormSubmit", null) as mshtml.IHTMLElement;
btnSubmit.click();
出现确认对话框(“您确定吗?”)。 我想以某种方式向 MSHTML 文档发送“Enter”键以自动确认提交。
I am trying to automatically parse/submit a web page using MSHTML (in C#.Net 3.1 WPF WebBrowser control). I can fill the forms, click buttons, and navigate pages without problems.
But I do not know how to automatically click the "OK" button on the JavaScript confirmation dialog which appears when I click the "Submit" button.
C# code:
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser.Document;
mshtml.IHTMLFormElement form = doc.forms.item("inputForm", 0) as mshtml.IHTMLFormElement;
mshtml.IHTMLElement btnSubmit = form.item("btnFormSubmit", null) as mshtml.IHTMLElement;
btnSubmit.click();
The confirmation dialog ("Are you sure?") appears.
I want to somehow send an "Enter" keystroke to the MSHTML document to automatically confirm the submission.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于找到了如何在 WebBrowser 控件中抑制确认消息的方法。 它实际上是在 javascript 函数 showConfirm() 中显示的。 我在 Browser.Document 标题的顶部插入虚拟函数 showConfirm() ,它会覆盖/抑制原始函数 showConfirm() 。 代码很繁忙,但主要思想很清晰,并且运行完美。
I finally found myself the way how to suppress confirmation message in WebBrowser control. It is actually shown in javascript function showConfirm(). I insert dummy function showConfirm() at the top of the header of the Browser.Document which overwrites/suppress the original function showConfirm(). The code is busy, but the main idea is clear, and works perfect.
由于 WebBrowser 控件是一个黑匣子,因此您可能应该研究通过 Win32 API 获取对话框的句柄。 从那里您可以调用“确定”按钮。 抱歉,我不能说得更具体,因为我以前从未做过这件事。 不过,希望这足以让您朝着正确的方向进行研究。
Since the WebBrowser control is a black box, you should probably research getting a handle to the dialog through the Win32 API. From there you can invoke the 'OK' button. Sorry I can't be more specific than that, as I've never done this particular thing before. Hopefully that's enough to get you researching in the right direction, though.