How to handle Message Box when using webbrowser in C #?

I use this webbrowswer feature in C #. Attempting to enter the website through my application. Everything goes well, except when an incorrect identifier or password is entered there, a small message box (which is installed on the web page itself) that pops up and blocks everything until the "OK" button is clicked.Message from webpage

So the question is: is there a way to control this small window (for example, reading text inside it)? If that's great then! But if there is no way to do this, is it just the same to make this message box a missing programmatically?

+4
source share
2 answers

"" , user32.dll . , OK:

public class Foo
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);


    private void ClickOKButton()
    {
        IntPtr hwnd = FindWindow("#32770", "Message from webpage");
        hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
        uint message = 0xf5;
        SendMessage(hwnd, message, IntPtr.Zero, IntPtr.Zero);
    }
}

MSDN.

+7

Sires anwser: fooobar.com/questions/296597/...

private void InjectAlertBlocker() {
    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
    HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
    string alertBlocker = "window.alert = function () { }";
    element.text = alertBlocker;
    head.AppendChild(scriptEl);
}
+2

All Articles