量角器-如何在父节点中查找所有子元素?

时间:2018-09-12 16:15:44

标签: angularjs protractor qa

我正在尝试查找父级Web元素内的所有子级元素。

这是父元素:

tableRow = element.all(by.xpath(
           "//tr[contains(@class, 'ng-scope')]" +
           "[@ng-repeat='audit in location.displayedProfileAudits']"))
          .get(0);

我尝试了以下代码片段,但都失败了:

//this fails with a timeout
var resultIcons = tableRow.all(by.xpath(".//i[contains(@class, 'icon')][contains(@ng-if, 'field.match')]"));

//this fails due to: TypeError: tableRow.element.all is not a function
var resultIcons = tableRow.element.all(by.xpath(".//i[contains(@class, 'icon')][contains(@ng-if, 'field.match')]"));

//this also times out
var resultIcons = tableRow.element(by.xpath(".//i[contains(@class, 'icon')][contains(@ng-if, 'field.match')]"));

任何人都可以以正确的方式指导/指导我进行处理吗?

谢谢

1 个答案:

答案 0 :(得分:0)

@kevin。我会使用CSS选择器。基本上,我认识的几乎每个人都使用CSS选择器。而且看起来非常简单。

例如:

var resultIcons = $$('your_parent_element your_child_element'))

示例:

<bl-e-comm tag="parent">  

 <div elem="child1">....</div>
 <div elem="child1">....</div>
 <div elem="child1">....</div>
 <div elem="child1">....</div>

</bl-e-comm>

您的定位器是:

var allChildElements = $$('bl-e-comm[tag="parent"] div') // $$('bl-e-comm[tag="parent"] {NOTE:here is empty space between locators} div')

将收集父级 bl-e-comm [tag =“ parent”]

的所有div元素

在此处阅读有关CSS的更多选项:https://www.w3schools.com/cssref/css_selectors.asp

将您的脚本转换为CSS选择器:

tableRow = element.all(by.css(
        "tr[class*='ng-scope')][ng-repeat*='audit in location.displayedProfileAudits']"))
        .get(0);

var resultIcons = tableRow.element(by.css("i.icon[ng-if*='field.match']"));