当我使用下面的代码时,它应该从网页获取数值,但是我得到的值是 [object][object]
下面是我正在使用的代码。
var resultText = element(by.id('output')).getAttribute();
console.log("Result text: " + resultText);
我也尝试过以下方法,但是我无法从网页中获取正确的值。
element(by.id('output')).each(function (element) {
var resultText = element.getAttribute('value').then(function(attr){
expect(typeof attr).toBe("string");
});
});
你能帮我获取元素的值吗?感谢您对此的帮助。
最佳答案
getAttribute()
和 getText()
总是返回一个 promise 。所以你应该使用它返回的 promise 来获取元素的值,然后控制台记录它或在你的期望语句中使用它。这是如何做到的 -
element(by.id('output')).getAttribute('value').then(function(resultText){
console.log(resultText);
expect(typeof resultText).toBe("string");
});
如果有更多元素的 id output
然后使用 .each()
方法来检索每个元素的值。
element.all(by.id('output')).each(function (ele) {
ele.getAttribute('value').then(function(attr){
expect(typeof attr).toBe("string");
console.log(attr); //print the value of returned by getAttribute
});
});
getText()
也以类似的方式工作。这是一个例子-
element(by.id('output')).getText().then(function(text){
console.log(text); //print the text that output element holds
});
有关 getAttribute() 的更多信息和 getText() .希望这会有所帮助。
关于javascript - 无法在 Protractor 中使用 getText() 和 getAttribute() 验证文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32522361/