views:

3148

answers:

4

I try to automatically parse/submit web page using MSHTML (in C#.Net 3.1 WPF WebBrowser control). I can fill the forms, click buttons, naviagate pages without problems. But do not know how to automatically click "ok" button on javascript confirmation dialog which appear when I click "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 somehow to send "Enter" keystroke to MSHTML document to automatically confirm the submission.

A: 

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.

Rex M
Thanks. I am looking for simpler way to click Ok button on confirm dialog using MSHTML and WebBrowser control. I can click anything but this annoying dialog window. I need somehow send "Enter" keystroke to HSHTML doc using Javascript
You can't do that using JavaScript - see this answer: http://stackoverflow.com/questions/563890/is-there-a-way-to-simulate-a-click-on-an-alert-in-javascript/563905#563905 ... confirm works the same way. That's why I suggest the Win32 API.
Rex M
A: 

please give me code

A: 

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.

A: 

Soliton,

I've been reading some of your posts from 2009. I too am trying to fill forms and inject javascript into a webpage through the WPF browser. It seems that you had the same frustration that I am experiencing -- not able to access the browser document (as in windows forms). From the information above, it sounds as if you might have solved the problem. Would you mind posting your solution for filling forms. If so, would you please include information about which events you for the browser and provide the associated code. Any assistance would be appreciated.

Jonathan