0

I have a web browser automation project written in WinForms C#. During the navigation there is a point where the browser does the "are you sure you want to leave this page?" popup. We need this popup, so I cannot remove it from the website code, which means I have to override it in my automation app.

Does anyone have an idea how to do this?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
ploink
  • 61
  • 1
  • 8
  • Ok i solved it by having a background thread sleeping for a a second and then doing a sendkeys.sendwait("{ENTER}") Not exactly smooth but it works.. – ploink Apr 27 '12 at 10:34

3 Answers3

2

and here was the smooth solution..

add a reference to mshtml and add using mshtml;

Browser.Navigated += 
    new WebBrowserNavigatedEventHandler( 
        (object sender, WebBrowserNavigatedEventArgs args) => { 
            Action<HtmlDocument> blockAlerts = (HtmlDocument d) => { 
                HtmlElement h = d.GetElementsByTagName("head")[0]; 
                HtmlElement s = d.CreateElement("script"); 
                IHTMLScriptElement e = (IHTMLScriptElement)s.DomElement; 
                e.text = "window.alert=function(){};"; 
                h.AppendChild(s); 
            }; 
            WebBrowser b = sender as WebBrowser; 
            blockAlerts(b.Document); 
            for (int i = 0; i < b.Document.Window.Frames.Count; i++) 
                try { blockAlerts(b.Document.Window.Frames[i].Document); } 
                catch (Exception) { }; 
        } 
    ); 
ploink
  • 61
  • 1
  • 8
  • I've actually provided an answer to this in one of my responses to some question somewhere (i'm the guy that wrote the "WebBrowser Control Limitations"). What is the purpose of your app, if I may ask? – Erx_VB.NExT.Coder Jul 21 '13 at 06:58
  • Also, did you find my answer with the window.alert=function(){}; in it or has someone else written about this as well? – Erx_VB.NExT.Coder Jul 21 '13 at 06:59
0

Are you able to make any changes to the website code?

If so, you might look at exposing an object through ObjectForScripting, then having the website code check window.external (and possibly interrogating your object) before it decides to display the popup - so if it can't find your object, it assumes it's being used normally and shows it.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • no unfortunately im not allowed to change anything (corporate bureaucrats) – ploink Apr 27 '12 at 10:14
  • http://stackoverflow.com/questions/1495944/webbrowser-control-limitations in this question its mentioned that it is possible but not how – ploink Apr 27 '12 at 10:15
0

Don't need add anymore. Try it. Work like a charm. ^_^

private void webNavigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        HtmlDocument doc = webBrowser.Document;
        HtmlElement head = doc.GetElementsByTagName("head")[0];
        HtmlElement s = doc.CreateElement("script");
        s.SetAttribute("text", "function cancelOut() { window.onbeforeunload = null; window.alert = function () { }; window.confirm=function () { }}");
        head.AppendChild(s);
        webBrowser.Document.InvokeScript("cancelOut");
    }
stuartd
  • 70,509
  • 14
  • 132
  • 163
MinhHD
  • 1