使用UIAutomation选择网格中存在的复选框

时间:2015-11-09 05:36:56

标签: c# ui-automation microsoft-ui-automation

使用UIAutomation选择网格中存在的复选框。下面的代码返回invoke模式的invalidpattern:

AutomationElement mainGrid = appElement1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "ReadinessTestList"));
// find the grid in the window 
if (mainGrid != null)
 {

    MessageBox.Show("inside the grid");

    // select just the first cell 
    var item = mainGridPattern.GetItem(0, 0);
    MessageBox.Show(Convert.ToString(item));
    //item.SetFocus();

    AutomationElement FirstCheckBox = GetTextElement(item, "SystemNameCheckBox");

    if (FirstCheckBox != null)
    {
        MessageBox.Show(Convert.ToString(FirstCheckBox));
        TogglePattern SelectedFirstCheckBox = FirstCheckBox.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern;
        MessageBox.Show(Convert.ToString(SelectedFirstCheckBox));

        ToggleState FirstCheckBxState = SelectedFirstCheckBox.Current.ToggleState;
        string try2 = Convert.ToString(FirstCheckBxState);
        MessageBox.Show(try2);

        //FirstCheckBxState.On;

        if (FirstCheckBxState != ToggleState.On) // not on? click it
        {            
            InvokePattern invokefirstCheckBox = (InvokePattern)FirstCheckBox.GetCurrentPattern(InvokePattern.Pattern);
            Thread.Sleep(2000);
            invokefirstCheckBox.Invoke();
        }

    }

1 个答案:

答案 0 :(得分:0)

您应该使用toggle pattern而不是调用模式作为复选框。

 TogglePattern tpToggle = (TogglePattern)aeElement.GetCurrentPattern(TogglePattern.Pattern);
 tpToggle.Toggle();

此外,您应该使用在安装windows development kit后可以在C:\ Program Files(x86)\ Windows Kits \ 8.1 \ bin \ x86中找到的inspect.exe。此工具将允许您查看您尝试自动化的支持模式。

相关问题