WatiN ie9确认对话框无效

时间:2011-07-01 07:34:15

标签: c# internet-explorer-9 watin

  

可能重复:
  Watin & IE9 - Cant click ok buttons

var dialogHandler = new WatiN.Core.DialogHandlers.ConfirmDialogHandler();
            using (new WatiN.Core.DialogHandlers.UseDialogOnce(browser.DialogWatcher, dialogHandler))
            {
                browser.Button(Find.ById("btnSave")).ClickNoWait();

                dialogHandler.WaitUntilExists();                                       
            }

它没有工作即9,javascript确认 我已经使用最新版本2.1

4 个答案:

答案 0 :(得分:0)

ConfirmDialogHandler confirmHandler = new ConfirmDialogHandler();
        using (new UseDialogOnce(browser.DialogWatcher, confirmHandler))
        {
            confirmHandler.WaitUntilExists();

            confirmHandler.CancelButton.Click();
        }

它适用于ie7,但不适用于ie 9 DrunkenMonkey的答案不起作用

华廷-2.1.0.1196

答案 1 :(得分:0)

如果您的确认对话框在IE9中不起作用,请尝试下一个决定

(Visual Studio 2010,Windows7,NetFramework 4.0,Internet Explorer 9)

首先,您必须将UIAutomationClient和UIAutomationTypes引用到您的测试项目中。

下一个方法扩展了Browser类

public static void ConfirmDialogIE9(this Browser browser)
    {
        browser.ShowWindow(NativeMethods.WindowShowStyle.ShowMaximized);
        Thread.Sleep(2000);
        System.Windows.Automation.TreeWalker trw = new System.Windows.Automation.TreeWalker(System.Windows.Automation.Condition.TrueCondition);
        System.Windows.Automation.AutomationElement mainWindow = trw.GetParent(System.Windows.Automation.AutomationElement.FromHandle(browser.hWnd));
        System.Windows.Automation.AutomationElementCollection main = mainWindow.FindAll(System.Windows.Automation.TreeScope.Children
       , System.Windows.Automation.Condition.TrueCondition);


        foreach (System.Windows.Automation.AutomationElement element in main)
        {
            if (element.Current.Name.Equals("VIIS - Windows Internet Explorer") && element.Current.LocalizedControlType == "pane")
            {

                System.Windows.Automation.AutomationElement DialogBox = trw.GetFirstChild(element);

                DialogBox.SetFocus();
                System.Windows.Automation.InvokePattern clickOk = (System.Windows.Automation.InvokePattern)
                DialogBox.FindAll(System.Windows.Automation.TreeScope.Children, System.Windows.Automation.Condition.TrueCondition)[0].GetCurrentPattern(System.Windows.Automation.AutomationPattern.LookupById(10000));
                clickOk.Invoke();
                Thread.Sleep(1000);
                break;

            }
        }

答案 2 :(得分:0)

我的项目中有以下代码:

var cancel = browser.Link(Find.ByUrl(CANCEL_LINK));
var confirmDialog = new ConfirmDialogHandler();
using (new UseDialogOnce(browser.DialogWatcher, confirmDialog))
{
    cancel.ClickNoWait();
    confirmDialog.WaitUntilExists();
    confirmDialog.OKButton.Click();
    browser.WaitForComplete();
}

这适用于IE9。请注意,它是WatiN v2.0.50727,但我没有看到这对你运行v2.1会产生影响。

答案 3 :(得分:-2)

Watin有处理对话框的方法。尝试使用这种方法:

public static void SetCloseIEHandler(bool clickOk)
{
    closeIeHandler = new CloseIEDialogHandler(clickOk);
    BaseIEController.IE.DialogWatcher.Add(closeIeHandler);
}

private static void ClearDialogHandler(IDialogHandler dialogHandler)
{
    if (BaseIEController.IE.DialogWatcher.Contains(dialogHandler))
    {
        BaseIEController.IE.DialogWatcher.Remove(dialogHandler);
        dialogHandler = null;
    }
}