【问题标题】:watin capture popup window and click "Yes"watin 捕获弹出窗口,然后单击“是”
【发布时间】:2026-01-07 18:00:01
【问题描述】:

我使用 watin 登录网站并检索信息。如果同一帐户在其他地方已经存在另一个登录名,则网站会弹出一个窗口并说“关闭其他会话?”并且有两个按钮“是”和“否”。我需要捕获它并单击“是”。

我不知道弹出窗口是什么类型,我尝试了 AlertDialogHandler 和 ConfirmDialogHandler,但似乎不起作用,有人可以帮忙吗?下面是代码片段。谢谢!

   BrowserIe.Ie.Image(Find.ByName("login")).ClickNoWait();

    AlertDialogHandler d= new AlertDialogHandler();
    if(d.Exists())
    {
        log.Debug("got alert dialog when logging in...");
        d.OKButton.Click();
    }

    ConfirmDialogHandler confirm = new ConfirmDialogHandler();
    if(confirm.Exists())
    {
        {
            log.Debug("got confirm dialog when logging in...");
           confirm.OKButton.Click();
        }

    }

【问题讨论】:

    标签: .net popup watin


    【解决方案1】:

    这应该可行:

    ConfirmDialogHandler confirm = new ConfirmDialogHandler();
    using (new UseDialogOnce(BrowserIe.DialogWatcher, confirm))
    {
        BrowserIe.Image(Find.ByName("login")).ClickNoWait();
        confirm.WaitUntilExists();
        confirm.OKButton.Click();
    }
    

    根据消息的速度,您甚至可能不需要 Wait 方法。

    给这只猫换皮的另一种方法是重写 JavaScript 确认函数,返回 true:

    BrowserIe.RunScript("function confirm(message) { return true; }");
    

    【讨论】:

    • 看来这行得通。就我而言,确认弹出窗口并不总是弹出,有时是,有时不是。所以我使用: if(confirm.exit()).... 你能更具体地说明如何覆盖 javascript 确认功能吗?我没有网站的代码。谢谢。
    【解决方案2】:

    我见过的例子都更像这样:-

    ConfirmDialogHandler confirm = new ConfirmDialogHandler();
    using (UseDialogOnce udo = new UseDialogOnce(BrowserIe.DialogWatcher, confirm))
    {
        dlgHnd.WaitUntilExists();
        confirm.OKButton.Click();
    }
    

    【讨论】: