C# WatiN ie9确认对话框不工作

C# WatiN ie9确认对话框不工作,c#,internet-explorer-9,watin,C#,Internet Explorer 9,Watin,可能重复: 它在IE9上不起作用,请确认 我已经使用了最新版本2.1,Watin有处理对话框的方法。尝试使用以下方法: public static void SetCloseIEHandler(bool clickOk) { closeIeHandler = new CloseIEDialogHandler(clickOk); BaseIEController.IE.DialogWatcher.Add(closeIeHandler); } private static voi

可能重复:

它在IE9上不起作用,请确认
我已经使用了最新版本2.1,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;
    }
}
它适用于ie7,但不适用于IE9 醉鬼的回答不起作用


WatiN-2.1.0.1196

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

(Visual studio 2010、Windows 7、NetFramework 4.0、Internet explorer 9)

首先,必须将Reference 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;

            }
        }

我的项目中有以下代码:

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会有什么影响。

ok它有时有效,有时无效……取消链接应该是什么?谢谢@buzzzzjay:
CANCEL_LINK
是一个
String
变量,用于保存要查找的链接的URL。查看
Find.ByUrl
中的intellisense,您会发现有不同的版本可供使用,这取决于映射链接时要使用的内容。您对如何将其与IE9一起使用有何建议?我一直收到一封信,因为它在30秒后超时,所以没有受到影响。
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;

            }
        }
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();
}