如果子控件的内部文本中是独一无二的,你可以随时搜索上,使用在定义父控件。例如:
public HtmlControl child()
{
HtmlControl parent = new HtmlControl(browser);
parent.SearchProperties["id"] = "[my id]";
HtmlControl child = new HtmlControl(parent);
child.SearchProperties["innerText"] = "[the inner text]";
return child;
}
如果你真的想遍历,那么你就必须爬行使用.GetParent()和.GetChildren()的UITestControl类的方法结构。
public HtmlControl child()
{
//First, we create an empty HtmlControl to return.
HtmlControl result = new HtmlControl()
//Specify the parent and get a collection of the children (this only goes one level,
// so if you have to go deeper, you'll have to nest your foreach loops and get
// children of the children, etc.
HtmlControl parent = new HtmlControl(browser);
parent.SearchProperties["id"] = "[my id]";
UITestControlCollection children = parent.GetChildren();
foreach (UITestControl child in children)
{
// If the child has the text you're looking for, then assign it to the result
// object and break the loop.
if (child.GetProperty("InnerText").ToString().Equals(searchTerm))
{
result = (HtmlControl)child;
break;
}
}
return result;
}
就我个人而言,我会尝试第一个选项。然而,你最好的选择是要求(礼貌地)让开发者在HTML中添加一些独特的和静态的标签。