我在 C# windowsform 程序中使用 webbrowser 控件,该程序浏览网站的多个页面,然后使用网站中的某些表单进行交易。 (我尝试使用 httpwebrequest
和 webclient
来做到这一点,但在使用 cookie 和复制网站如何动态生成某些表单选择选项时遇到了困难。我决定使用网络浏览器并利用网站的脚本 - 这不是我的网站)。
在最后的步骤之一中,我到达了一个带有表单的页面,在该页面上,网站在提交表单时在页面上运行验证脚本。如果用户输入的信息不正确,则会弹出警报。
但是当我在我的程序中导航到该页面时(在我为空字段赋值之前),我收到了警报。当我使用 Chrome、Firefox 或 IE 手动执行此操作时,不会发生这种情况。但它发生在网络浏览器中。我可以通过提交包含未验证信息的表单来在常规浏览器中复制此内容 - 但在网络浏览器中,它会在我加载页面时发生。
我的目标是:
检测到弹出警告已出现并获得焦点。 (警报的名称是“来自网页的消息”。)
单击警报的“确定”按钮,让我的程序继续输入信息并继续到流程结束。
这里有几个问题和我的类似。我发现最有前途的帖子具有以下代码:
[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
函数。但它不起作用。
所以我有几个问题:
还有其他方法可以解决警报弹出窗口吗?
假设这段代码有意义,在调用这段代码之前我可以运行什么样的条件测试(我如何检查警报是否出现?)
页面在上一页表单的
InvokeMember("submit")
命令后加载,也就是出现警报时。提交后我的代码中的下一步是一个 documentcompleted 事件处理程序,它触发然后完成新表单。这几乎就像网络浏览器在我完成字段之前提交表单一样。因此,我不知道在哪里插入此ClickOKButton
代码。在我发现的代码中我不明白的事情中,我不明白传递给 FindWindow 的“#32770”参数。我怎么知道这是否适合我的警报?
最佳答案
我写了下面的代码,它运行得很完美。创建的控制台应用程序是单击 javasript 警报/确认框的确定按钮。
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!");
}
}
}
}
关于javascript - C# 在 web 浏览器控件中单击 javascript 警报消息确定按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42065185/