简体   繁体   中英

C# clicking a javascript alert message OK button in webbrowser control

I am using a webbrowser control in a C# windowsform program that navigates through several pages of a website and then uses some of the forms in the website to conduct a transaction. (I tried to do this with httpwebrequest and webclient , but ran into difficulties with cookies and replicating some of how the website dynamically generates some of the form selection choices. I decided to use the webbrowser and take advantage of the website's scripting - it is not my site).

In one of the final steps I reach a page with a form where the site runs a validation script on a page when the form is submitted. If the user has entered incorrect info an alert pops up.

But when I navigate to that page in my program (before I give values to the empty fields), I get the alert. This doesn't happen when I do it manually with Chrome, Firefox or IE. But it happens in the webbrowser. I can replicate this in a regular browser by submitting the form with info that doesn't validate - but in web browser it happens when I load the page.

My goal is to:

  1. detect that the popup alert has appeared and taken focus. (The name of the alert is "Message from webpage.")

  2. click the OK button of the alert, and let my program proceed to enter the information and continue through the end of the process.

There are several questions similar to mine here. The most promising post i found has the following code:

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

This code is a little beyond my newbie understanding - I tried setting it up in a new class and then instantiating an object of this class I navigate to the page in question and then invoking the ClickOKButton method. Didn't work. I also tried including it at the form level and then running the ClickOKButton function at the point in the program where I navigate to the page where the alert appears. But it is not working.

So I have a few questions:

  1. is there another way to address the alert popup?

  2. assuming that this code makes sense, what kind of conditional test can I run before invoking this code (how can I check that the alert has appeared?)

  3. The page loads after an InvokeMember("submit") command of the previous page's form, and that is when the alert appears. The next step in my code after the submit is a documentcompleted event handler which fires then completes the new form. It is almost as if the webbrowser is submitting the form before I complete the fields. Because of this I have no idea where to insert this ClickOKButton code.

  4. Of the things I do not understand in the code I found, I don't understand the "#32770" parameter that is being passed to FindWindow. How can I know if that is right for my alert?

I wrote following code and it was working flawlessly. Created console app it was clicking on Ok button of the javasript Alert/confirm box.

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

    }
}
}

Try using

webBrowser.Document.ActiveElement.InvokeMember("click");

to automatically click on alert box. It worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM