如何从元素中获取孩子?

问题描述

我在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;

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

解决方法

我能够这样解决,我忘了设置真实条件,在这里我使用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);
                            }
                       ```

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...