MSHTML - Auto Click for JavaScript confirm dialog

∥☆過路亽.° 提交于 2019-12-14 02:29:07

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/588179/mshtml-auto-click-for-javascript-confirm-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!