量角器从 ElementArrayFinder 检索单个元素
Protractor retrieve single element from ElementArrayFinder
我想从符合条件的 ElementArrayFinder 中检索第一个元素。以下代码为我提供了我期望的元素,但它遍历了所有需要时间的元素。
有什么办法吗?
return content.all(by.css('.class')).filter(function(label){
return label.getText().then(function(text){
return text === searchName;
});
}).first();
结论:
Sudharsan Selvaraj 提供的答案很有魅力
var ele = content.all(by.xpath(".//*[contains(@class,'class')][normalize-space(text())='some-value']")).first()
搜索元素的时间大大减少
Florent B. 提供的答案也解决了问题,看起来是一种更简单的方法。
return content.element(by.cssContainingText('.class', searchName));
为什么不直接访问标签的第一个元素?
return content.all(by.css('.class label')).first().getText().then(function (text) {
return text === searchName;
});
P.S。我没有测试代码,但它应该可以工作
您可以使用 xpath 在下面的示例 xpath 中找到具有特定 text.Look 的元素。
var ele = content.all(by.xpath(".//*[contains(@class,'class')][normalize-space(text())='some-value']")).first()
您应该使用定位器 by.cssContainingText
通过 CSS 和文本查找元素:
return content.element(by.cssContainingText('.class', searchName));
我想从符合条件的 ElementArrayFinder 中检索第一个元素。以下代码为我提供了我期望的元素,但它遍历了所有需要时间的元素。
有什么办法吗?
return content.all(by.css('.class')).filter(function(label){
return label.getText().then(function(text){
return text === searchName;
});
}).first();
结论: Sudharsan Selvaraj 提供的答案很有魅力
var ele = content.all(by.xpath(".//*[contains(@class,'class')][normalize-space(text())='some-value']")).first()
搜索元素的时间大大减少
Florent B. 提供的答案也解决了问题,看起来是一种更简单的方法。
return content.element(by.cssContainingText('.class', searchName));
为什么不直接访问标签的第一个元素?
return content.all(by.css('.class label')).first().getText().then(function (text) {
return text === searchName;
});
P.S。我没有测试代码,但它应该可以工作
您可以使用 xpath 在下面的示例 xpath 中找到具有特定 text.Look 的元素。
var ele = content.all(by.xpath(".//*[contains(@class,'class')][normalize-space(text())='some-value']")).first()
您应该使用定位器 by.cssContainingText
通过 CSS 和文本查找元素:
return content.element(by.cssContainingText('.class', searchName));