4

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 little message box (that is set on the webpage itself) which pops up and blocks everything until "Ok" is clicked.Message from webpage

So the question is: Is there any possible way to manage this little window (like reading the text inside of it)? If it is then great! But if there's no way to do that then is there anyway to simply make this message box go away programatically?

Emil
  • 555
  • 3
  • 7
  • 16
  • Is there a specific reason why you are not simply POSTing the form directly to the webserver without using the webbrowser control? – Daniel Hilgarth Mar 19 '12 at 12:54
  • aha... Care to share it? – Daniel Hilgarth Mar 19 '12 at 12:59
  • 1
    Yeah, cant understand either why you are using a webbrowser control, if, obviously, you want to automate something. – squelos Mar 19 '12 at 13:00
  • 1
    lol, I love that response. He is asking that question Emil because, are you sure there isn't a better way to access these details/page/data rather then embedding a webbrowser into your application? This approach is prone to errors and I think you may find that you won't be able to do much to this alert without ALLOT of hackery – Maxim Gershkovich Mar 19 '12 at 13:01
  • possible duplicate of [Blocking dialogs in .NET WebBrowser control](http://stackoverflow.com/questions/77659/blocking-dialogs-in-net-webbrowser-control) – Daniel Hilgarth Mar 19 '12 at 13:14

3 Answers3

9

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.

Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
  • Wow! Great man! It worked pretty fine! But what about reading the context of that? (Like reading whether "Login is incorrect" or "Password is incorrect" ? – Emil Mar 19 '12 at 13:36
  • @Emil, see [WM_GETTEXT](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632627(v=vs.85).aspx) – Saeb Amini Mar 19 '12 at 16:05
2

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);
}
Community
  • 1
  • 1
maka
  • 566
  • 4
  • 11
0

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

    }
}
}
Suhas Dhongade
  • 79
  • 2
  • 12