How to handle Message Boxes while using webbrowser in C#?

后端 未结 3 1537
南方客
南方客 2021-01-01 05:43

I\'m using this webbrowswer feature of C#. Trying to log in a website through my application. Everything goes fine, except when a wrong ID or password is entered there\'s li

相关标签:
3条回答
  • 2021-01-01 06:04

    You can "manage" the message box dialog by importing some window functions from user32.dll and getting the messagebox dialog's handle by it's class name and window name. For example to click its OK button:

    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);
        }
    }
    

    Some reading material from MSDN.

    0 讨论(0)
  • 2021-01-01 06:06

    here is refined version of Saeb's answer. Saeb's code was not working for me, I added one more step to activate the button and then clicking on it.

    using System;
    using System.Runtime.InteropServices;
    
    namespace IE_Automation
    {
    public class IEPoppupWindowClicker
    {
        [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, int wParam, int lParam);
        private const int BM_CLICK = 0xF5;
        private const uint WM_ACTIVATE = 0x6;
        private const int WA_ACTIVE = 1;
    
        public void ActivateAndClickOkButton()
        {
            // find dialog window with titlebar text of "Message from webpage"
    
            var hwnd = FindWindow("#32770", "Message from webpage");
            if (hwnd != IntPtr.Zero)
            {
                // find button on dialog window: classname = "Button", text = "OK"
                var btn = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
                if (btn != IntPtr.Zero)
                {
                    // activate the button on dialog first or it may not acknowledge a click msg on first try
                    SendMessage(btn, WM_ACTIVATE, WA_ACTIVE, 0);
                    // send button a click message
    
                    SendMessage(btn, BM_CLICK, 0, 0);
                }
                else
                {
                    //Interaction.MsgBox("button not found!");
                }
            }
            else
            {
                //Interaction.MsgBox("window not found!");
            }
    
        }
    }
    }
    
    0 讨论(0)
  • 2021-01-01 06:08

    Copy paste from Sires anwser: https://stackoverflow.com/a/251524/954225

    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);
    }
    
    0 讨论(0)
提交回复
热议问题