C# WatiN - Add an AlertDialogHandler to click ok button on every Alert dialog window

白昼怎懂夜的黑 提交于 2019-11-29 02:01:14

Create class:

public class OKDialogHandler : BaseDialogHandler
{
    public override bool HandleDialog(Window window)
    {
        var button = GetOKButton(window);
        if (button != null)
        {
            button.Click();
            return true;
        }
        else
        {
            return false;
        }
    }

    public override bool CanHandleDialog(Window window)
    {
        return GetOKButton(window) != null;
    }

    private WinButton GetOKButton(Window window)
    {
        var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.ClassName == "Button" && new WinButton(w.Hwnd).Title == "OK").FirstOrDefault();
        if (windowButton == null)
            return null;
        else
            return new WinButton(windowButton.Hwnd);
    }
}

After creating instance of IE, attach dialog handler to it:

ie.AddDialogHandler(new OKDialogHandler());

This dialog handler will handle all windows, that contains a button with "OK" caption, by clicking on that button.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!