且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中使用InnerText递归查找UIElement

更新时间:2023-11-10 11:26:04

在我曾经使用过的地方基于以下代码。它会找到所有 InnerText 项。

In one place I used code based on that below. It finds all InnerText items.

someControl.SearchProperties.Add("InnerText", "", PropertyExpressionOperator.Contains);
UITestControlCollection colNames = someControl.FindMatchingControls();

在另一个地方,我使用了:

In another place I used:

string s = "";  // In case there is no InnerText.
try
{
    s = control.GetProperty("Text").ToString();
}
catch ( System.NotSupportedException )
{
    // No "InnerText" here.
}

该异常未记录在 GetProperty ,我想我在对确实执行此操作的控件上调用该方法时发现了它没有 InnerText 。我找不到任何 TryGetPropertyMethod ,但是编写自己的代码很容易。

The exception is not documented with GetProperty, I think I found it when calling the method on controls that did not have an InnerText. I could not find any TryGetPropertyMethod, but it would be easy to write your own.

我还使用了基于此递归例程的代码来访问层次结构中的所有控件。

I also used code based on this recursive routine to visit all the controls in the hierarachy.

private void visitAllChildren(UITestControl control, int depth)
{
    UITestControlCollection kiddies = control.GetChildren();

    foreach ( UITestControl kid in kiddies )
    {
        if ( depth < maxDepth )
        {
            visitAllChildren(kid, depth + 1);
        }
    }
}