我有这个功能
var goThrough = function(opt_parentElement) {
var using = opt_parentElement;
var matches = [];
var addMatchingLeaves = function(element) {
if (element.children) {
if (element.children.length === 0) {
matches.push(element);
}
for (var i = 0; i < element.children.length; ++i) {
addMatchingLeaves(element.children[i]);
}
}
};
addMatchingLeaves(using);
return matches;
};
然后我在测试中称它为
amountOfElements = goThrough(element(by.css('.form-group')));
html本身就是
<div class="form-group has-feedback">
<input id="password" name="password" type="password">
</div>
问题是函数看不到 .form-group 的子项。我试过其他元素,但结果是一样的,函数不会首先进入“if”
最佳答案
element(by.css('.form-group'))
返回 ElementFinder
,而不是您需要查询其 children
的 DOM 元素。如果您有很多元素但可以完成工作,这会有点慢:
var getLeafs = function(node) {
return node.all(by.css('*')).filter(function(child) {
return child.all(by.xpath('*')).count().then(function(childrenCount) {
return childrenCount === 0;
});
});
};
它返回 node
上下文中没有 child 的元素。
用法:
var leafs = getLeafs(element(by.css('.form-group')));
leafs.count().then(function(c) {
console.log(c);
});
关于javascript - Protractor 看不到元素的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30999173/