Ask to my C# application to wait until an action is taken on a Javascript Alert using mshtml

Posted on 2008-10-23
Last Modified: 2013-12-17
Hi I am bulding an application that opens an IE browser and fill fields out automaticaly using Microsoft.mshtml but when it comes to click on the Next button an Javascript alert open confirming the adress with a yes/no.

How to, I either tell the application to click Yes automaticaly then wait the action to click on the Next button again

My main issue is to wait till the user click yes on the alert windows then tell the application to click on the next button again.

If you could fix both of my problem, it would be awsome.

any ideas??


Here is my code snippet:
Object o = null;

            Object URL = "";

            mshtml.HTMLDocumentClass doc;

            InternetExplorer ie = new InternetExplorer();

            ie.Visible = true;

            ie.Navigate2(ref URL, ref o, ref o, ref o, ref o);

            while (ie.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)





            // get the document

            doc = (mshtml.HTMLDocumentClass)ie.Document;

            mshtml.HTMLInputElementClass el = null;

            // Login

            ieValue("ctl00$CPPC$Username", "username", el, doc); //

            ieValue("ctl00$CPPC$Password", "password", el, doc); //

            ieClick("ctl00$CPPC$imgLogin", el, doc); //         

                // Check if shipTo Canada, Else fill field, show Browser

                if (d1_ctl00_CPPC_ctrlShipToAddress_ddlCountry.Text == "Canada")


                    // Filling text box from the first page.

                    fillTextBox("p1_", el, doc);

                    // Wait If Have to change Shipped From

                    if (ch_fromDefault.Checked != true)


                        ieClick("ctl00$CPPC$btnNext", el, doc); //

//this is at this point that the alert appear. After clicking yes. I need to call ieClick("ctl00$CPPC$btnNext", el, doc); // again




                        ie.Visible = true;

                        ieJavascript("ctrlShipFromAddress_Edit();", el, ie);






                    fillTextBox("p1_", el, doc);




Open in new window

Question by:maxleb

Accepted Solution

AshleighGreen earned 500 total points
ID: 22990333

Get the user to confirm (Yes/No) with a confirmation box.

var answer = confirm("Do you want to click next?"

you can then use answer:
  //click button that causes page load

That way the action only happens if the user clicks ok in the confirmation box.

I know what I've written is in javascript, but it should be pretty easy to convert to mshtml if required. You could also leave it as JS and make it a function you call from mshtml. Plenty of options, but that's the general technique I'd recommend using to trigger .net actions such as confirmation boxes from javascript.

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now