Как получить родительский элемент и все его дочерние элементы с помощью UIAutomationClient? - PullRequest
0 голосов
/ 17 июня 2020

Я хочу извлечь все ошибки доступности из документа MS-Word. Я могу получить отчет об ошибке верхнего уровня с помощью приведенного ниже кода. Однако я не могу получить всю ошибку вместе со списком внутри ошибки. Как получить все ошибки уровня root вместе со всеми элементами внутри верхнего уровня в соответствии с инструментом проверки?

Вывод моего приложения:

Ошибки
Отсутствует альтернативный текст (3)
Изображение или объект не встроены (1)

Мой ожидаемый результат:

Ошибки
Отсутствует альтернативный текст (3)

Изображение 1, Отсутствует альтернативный текст
Рисунок 2, Отсутствует альтернативный текст
Рисунок 3, Альтернативный текст отсутствует текст

Изображение или объект не в строке (1)
Изображение 3, изображение или объект не в строке

Мой тест Ms -Отчет о доступности документа Word:

enter image description here

Структура в инструменте Inspect.exe

enter image description here

Мой код следующий:

using System;
using UIAutomationClient;

namespace UIAutomationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IUIAutomation uiAutomation = new CUIAutomation();

            IUIAutomationElement rootElement = uiAutomation.GetRootElement();

            // Assume the first child of the root element with a ClassName of "OpusApp" is the Word window we're interested in.
            int propertyIdClassName = 30012; // UIA_ClassNamePropertyId
            IUIAutomationCondition conditionWordApp = uiAutomation.CreatePropertyCondition(propertyIdClassName, "OpusApp");
            IUIAutomationElement wordElement = rootElement.FindFirst(TreeScope.TreeScope_Children, conditionWordApp);

            if (wordElement == null)
            {
                throw new Exception("MS Word file must be opened. Please open the file and try again.");
            }

            int propertyIdName = 30005; // UIA_NamePropertyId

            //Errors
            IUIAutomationCondition errorssCondition = uiAutomation.CreatePropertyCondition(propertyIdName, "Errors");
            var errorsInspectionResults = wordElement.FindFirst(TreeScope.TreeScope_Subtree, errorssCondition);

            IUIAutomationTreeWalker errorWalker = uiAutomation.ControlViewWalker;
            IUIAutomationElement errorsElement = errorWalker.GetFirstChildElement(errorsInspectionResults);
            Console.WriteLine("Errors\n------------------------------------\n");
            while (errorsElement != null)
            {
                Console.WriteLine(errorsElement.CurrentName);
                errorsElement = errorWalker.GetNextSiblingElement(errorsElement);
            }
            Console.ReadKey();
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...