SetFocus()不适用于使用UIAutomation的Excel 2016控件

时间:2019-02-14 06:05:48

标签: ui-automation microsoft-ui-automation

我是UIAutomation的新手。我正在尝试自动化excel 2016以添加第三方插件。 当我在任何Excel控件上使用setfocus()时,将引发异常“目标元素无法获得焦点”。

我试图在文本框中输入值,并观察到该文本框不支持ValuePattern,所以我想到了发送键盘笔触。在输入值之前,我试图在文本框上使用SetFocus,但是它引发了异常。您能否让我知道SetFocus如何进行控制。还是有其他不需要我使用键盘笔触的地方。

请找到以下代码。

    public void TestMethod1()
    {
        var mouse = Mouse.Instance;
        var keyboard = Keyboard.Instance;
        AutomationElement desktop = AutomationElement.RootElement;
        PropertyCondition condtion = new PropertyCondition(AutomationElement.NameProperty, "Book1 - Excel");
        AutomationElement ExcelWindow = desktop.FindFirst(TreeScope.Children, condtion);         
        var fileMenu = ExcelWindow.FindFirst(TreeScope.Descendants, UIAutomation.GetCondtion(AutomationElement.NameProperty, "File Tab"));

        (UIAutomation.GetPattern<InvokePattern>(fileMenu, InvokePattern.Pattern)).Invoke();

        var options = ExcelWindow.FindFirst(TreeScope.Descendants, UIAutomation.GetCondtion(AutomationElement.NameProperty, "Options"));
        (UIAutomation.GetPattern<InvokePattern>(options, InvokePattern.Pattern)).Invoke();


        var trustCenter = ExcelWindow.FindFirst(TreeScope.Descendants, UIAutomation.GetCondtion(AutomationElement.NameProperty, "Trust Center"));
        (UIAutomation.GetPattern<SelectionItemPattern>(trustCenter, SelectionItemPattern.Pattern)).Select();


        var TrustCenterSetting = ExcelWindow.FindFirst(TreeScope.Descendants, UIAutomation.GetCondtion(AutomationElement.NameProperty, "Trust Center Settings..."));
        (UIAutomation.GetPattern<InvokePattern>(TrustCenterSetting, InvokePattern.Pattern)).Invoke();

        Thread.Sleep(2000);
        var trustCenterAddinCatalog = ExcelWindow.FindFirst(TreeScope.Descendants, UIAutomation.GetCondtion(AutomationElement.NameProperty, "Trusted Add-in Catalogs"));
        (UIAutomation.GetPattern<SelectionItemPattern>(trustCenterAddinCatalog, SelectionItemPattern.Pattern)).Select();

        var catlogUrl = ExcelWindow.FindFirst(TreeScope.Descendants, UIAutomation.GetCondtion(AutomationElement.NameProperty, "Trust Center")).FindFirst(TreeScope.Descendants, UIAutomation.GetCondtion(AutomationElement.NameProperty, "Catalog Url"));
        catlogUrl.SetFocus();
        keyboard.Enter("%+U");
        keyboard.Enter(@"\\INBLR10215WCPR\Excelmanifest");
}

0 个答案:

没有答案