Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
点击webbrowser控件中的javascript警报消息OK按钮_Javascript_C#_Webbrowser Control_Alert - Fatal编程技术网

点击webbrowser控件中的javascript警报消息OK按钮

点击webbrowser控件中的javascript警报消息OK按钮,javascript,c#,webbrowser-control,alert,Javascript,C#,Webbrowser Control,Alert,我在一个C#windowsform程序中使用webbrowser控件,该程序可以浏览网站的多个页面,然后使用网站中的一些表单进行交易。(我曾尝试使用httpwebrequest和webclient来实现这一点,但在cookie和复制网站动态生成某些表单选择的一些方式时遇到了困难。我决定使用webbrowser并利用网站的脚本-它不是我的网站) 在最后的一个步骤中,我访问了一个带有表单的页面,在提交表单时,站点会在页面上运行验证脚本。如果用户输入的信息不正确,将弹出警报 但当我在程序中导航到该页面

我在一个C#windowsform程序中使用webbrowser控件,该程序可以浏览网站的多个页面,然后使用网站中的一些表单进行交易。(我曾尝试使用
httpwebrequest
webclient
来实现这一点,但在cookie和复制网站动态生成某些表单选择的一些方式时遇到了困难。我决定使用webbrowser并利用网站的脚本-它不是我的网站)

在最后的一个步骤中,我访问了一个带有表单的页面,在提交表单时,站点会在页面上运行验证脚本。如果用户输入的信息不正确,将弹出警报

但当我在程序中导航到该页面时(在我给空字段赋值之前),我会收到警报。这在我用Chrome、Firefox或IE手动操作时不会发生,但在webbrowser中会发生。我可以在常规浏览器中通过提交带有未验证信息的表单来复制此功能,但在web浏览器中,当我加载页面时会发生这种情况

我的目标是:

  • 检测弹出警报是否已出现并已聚焦。(警报的名称为“来自网页的消息”。)

  • 单击警报的“确定”按钮,让我的程序继续输入信息,并继续完成整个过程

  • 这里有几个与我类似的问题。我发现的最有希望的帖子有以下代码:

    [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);
        }
    
    这段代码有点超出了我的新手理解——我尝试在一个新类中设置它,然后实例化这个类的一个对象,我导航到相关页面,然后调用
    ClickOKButton
    方法。没用。我还尝试将其包含在表单级别,然后在程序中导航到出现警报的页面的位置运行
    ClickOK Button
    功能。但它不起作用

    所以我有几个问题:

  • 是否有其他方法处理警报弹出窗口

  • 假设这段代码有意义,在调用这段代码之前我可以运行什么样的条件测试(我如何检查警报是否出现?)

  • 页面在上一页表单的
    InvokeMember(“submit”)
    命令后加载,此时会出现警报。提交之后,我的代码中的下一步是documentcompleted事件处理程序,该处理程序将激发并完成新表单。就好像webbrowser在我完成字段之前提交表单一样。因此,我不知道在何处插入此
    单击确定按钮
    代码

  • 在我找到的代码中,有一些我不理解的地方,我不理解传递给FindWindow的“#32770”参数。我怎么知道我的警告是否正确

  • 试用

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

    要自动单击警报框。它为我工作。

    我编写了以下代码,它工作得非常完美。创建控制台应用程序时,单击javasript警报/确认框的Ok按钮

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

    您需要使用Spy++了解有关警报窗口的更多信息,以便获取对它的引用。这个数字可能只与发布它的人的电脑有关。编辑:我知道我错了。当我在弹出窗口中使用它时,我会得到句柄号、标题和类:#32770(对话框)。因此,这个数字就是您正在搜索的窗口类。已解决-使用上面的代码。1-FindWindow、FindWindowEx和SendMessage的3个DLL引用和指针以及ClickOKButton函数都进入了in Form1类。2-不需要条件测试,因为FindWindow参数仅限于类#32770和“来自网页的消息”的名称。亚历山大在32770左右是对的。4-我将ClickOKButton命令放在页面和警报的InvokeMember(“submit”)命令之后。等待documentcomplete事件处理程序启动为时已晚-警报已弹出。希望这对其他人有帮助。我们需要调用ClickOKButton();方法?还是参考它?我只看到它是书面的,但从未调用过该方法。