【问题标题】:C# clicking a javascript alert message OK button in webbrowser controlC#单击webbrowser控件中的javascript警报消息OK按钮
【发布时间】:2017-02-06 10:13:33
【问题描述】:

我在 C# windowsform 程序中使用 webbrowser 控件,该程序浏览网站的多个页面,然后使用网站中的某些表单进行交易。 (我尝试使用httpwebrequestwebclient 来做这件事,但是在使用cookie 和复制网站如何动态生成一些表单选择选项时遇到了困难。我决定使用网络浏览器并利用网站的脚本- 这不是我的网站)。

在最后一个步骤中,我到达一个带有表单的页面,当提交表单时,站点在页面上运行验证脚本。如果用户输入了错误的信息,则会弹出警报。

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

我的目标是:

  1. 检测到弹出警报已出现并获得焦点。 (警报名称为“来自网页的消息”。)

  2. 点击alert的OK按钮,让我的程序继续输入信息,一直到流程结束。

这里有几个类似于我的问题。我发现的最有前途的帖子有以下代码:

[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 方法。没用。我还尝试在表单级别包含它,然后在程序中导航到出现警报的页面的位置运行ClickOKButton 函数。但它不起作用。

所以我有几个问题:

  1. 还有其他方法可以解决弹出警报吗?

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

  3. 在前一页表单的InvokeMember("submit") 命令之后加载页面,此时出现警报。提交后我的代码中的下一步是一个 documentcompleted 事件处理程序,它触发然后完成新表单。就好像网络浏览器在我完成字段之前提交表单一样。因此,我不知道在哪里插入这个ClickOKButton 代码。

  4. 在我发现的代码中我不理解的内容中,我不理解传递给 FindWindow 的“#32770”参数。我怎么知道这是否适合我的警报?

【问题讨论】:

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

标签: javascript c# webbrowser-control alert


【解决方案1】:

我编写了以下代码,它运行良好。创建控制台应用程序,它正在单击 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!");
        }

    }
}
}

【讨论】:

    【解决方案2】:

    尝试使用

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

    自动点击警报框。它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多