MSHTML - 自动点击JavaScript确认对话框 [英] MSHTML - Auto Click for JavaScript confirm dialog
问题描述
我正在尝试使用MSHTML自动解析/提交网页(在C#.Net 3.1 WPF WebBrowser控件中)。我可以填写表单,点击按钮和导航页面,而不会出现问题。
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.
但是我不知道如何自动点击JavaScript确认对话框上的确定按钮当我点击提交按钮时出现。
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#代码:
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();
出现确认对话框(Are you sure?)。
我想以某种方式向MSHTML文档发送Enter按键以自动确认提交。
The confirmation dialog ("Are you sure?") appears. I want to somehow send an "Enter" keystroke to the MSHTML document to automatically confirm the submission.
推荐答案
我终于找到了自己如何在WebBrowser控件中抑制确认消息的方式。它实际上在javascript函数showConfirm()中显示。我将虚拟函数showConfirm()放在Browser.Document的头部顶部,该文档覆盖/禁止原始函数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.
这篇关于MSHTML - 自动点击JavaScript确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!