?
Solved

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

Posted on 2008-10-23
3
Medium Priority
?
920 Views
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??

Thanks

Here is my code snippet:
Object o = null;
            Object URL = "https://eshiponline.purolator.com/ShipOnline/SecurePages/Public/FormsLogin.aspx";
            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)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(50);
            }
 
            // 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
 
                    }
                    else
                    {
                        ie.Visible = true;
                        ieJavascript("ctrlShipFromAddress_Edit();", el, ie);
                    }
                    
                }
                else
                {
                    fillTextBox("p1_", el, doc);
                    Application.Exit();
                    return;
                }

Open in new window

0
Comment
Question by:maxleb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
AshleighGreen earned 2000 total points
ID: 22990333
Hello,

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

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

you can then use answer:
i.e.
if(answer)
{
  //click button that causes page load
  document.getElementById('btnNext').click();
}

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.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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