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

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

使用 UIAutomation 选择网格中的复选框。下面的代码为调用模式返回无效模式:

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

    }

最佳答案

你应该使用 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。 .该工具将允许您查看您尝试自动化的受支持模式。

关于c# - 使用 UIAutomation 选择网格中存在的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603099/

相关文章:

angularjs - 用于 Angular E2E测试的 Protractor : What is the best approach to manage test data?

c# - MS UI 自动化 - 如何从 ControlType.text 获取文本

delphi - 使用 Delphi 通过 UIAutomation 获取底层菜单项的列表

c# - 在后台应用程序中读取字符

c# - C#中的大写句子

c# - 获取硬件信息

javascript - 不同的元素正在接收点击,即使 Xpath 是正确且唯一的(Protractor)

testing - 如何在 testcafe 命令行中提供嵌套文件夹路径?

c# - 为什么在 UI 自动化客户端中捕获的异常会出现在正在自动化的 UI 中?

c# - SWIG、Box2D 和 C#