【问题标题】:How to get children from element?如何从元素中获取孩子?
【发布时间】:2021-01-05 04:47:51
【问题描述】:

我在 IUIAutomationElement elem 中有我的元素,现在我想获取它的子元素,

为此,我已尝试遵循此代码

IUIAutomationElement* dummy = NULL;     //creating a dummy
IUIAutomationElementArray* children_array;   //creating an array 
elem->GetCachedChildren(&children_array); 
children_array->GetElement(0,&dummy);
qDebug() << dummy;

它不起作用,我得到未处理的异常。我哪里错了?

【问题讨论】:

  • 什么异常?也许 children_array 只是空的。
  • @SimonMourier 它肯定有一些元素。得到未处理的异常,在这里我正在创建一个变量 dummy 并且我假设 children_array 中第 0 位的元素将存储在 dummy 中,当我这样做时 children_array-&gt;GetElement(0,&amp;dummy);
  • 请为每个函数调用添加错误检查,看看在这个“unhandled exceptions”之前是否有函数失败。
  • @RitaHan-MSFT,改变了它,现在我没有得到例外但仍然没有孩子的名字
  • @Danzow get_CurrentName 成功但child_name 为空?

标签: c++ winapi ui-automation microsoft-ui-automation


【解决方案1】:

我能够像这样解决它,我忘记设置真实条件,在这里我使用GetCachedChildren()而不是FindAll(),因为这样可以更好地达到目的。

IUIAutomationElementArray* children_array = NULL;
                            IUIAutomationElement* single_elem = nullptr;
                            TreeScope treeScope_1 = TreeScope::TreeScope_Children;

                            IUIAutomationCondition* Condition;
                            automation->CreateTrueCondition(&Condition);

                            HRESULT hs = elem->FindAll(treeScope_1, Condition, &children_array);
                           // HRESULT hs = elem->GetCachedChildren(&children_array);
                            if (SUCCEEDED(hs) && children_array != NULL) {
                                int number = 0;
                                children_array->get_Length(&number);
                                for (int i = 0;i < number;i++)
                                {
                                    children_array->GetElement(i, &single_elem);
                                    if (single_elem != NULL)
                                    {
                                        BSTR child_name = NULL;
                                        hs = single_elem->get_CurrentName(&child_name);
                                        if (SUCCEEDED(hs) && child_name != NULL)
                                        {
                                            std::wstring child_ws(child_name, SysStringLen(child_name));
                                            QString child_qstring = QString::fromStdWString(child_ws);
                                            global_ui->xml_scripts_textbox->addItem(child_qstring);
                                            qDebug() << child_qstring;
                                        }
                                        SysFreeString(child_name);
                                    }
                                    single_elem = NULL;
                                }
                                SAFE_RELEASE(children_array);
                            }
                       ```

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 2019-02-19
    • 2013-08-03
    • 2019-12-17
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    相关资源
    最近更新 更多