【问题标题】:Getting a specific element from an ElementArrayFinder giving me issues从 ElementArrayFinder 获取特定元素给我带来了问题
【发布时间】:2017-04-06 08:22:12
【问题描述】:

这是Get a specific element from an ElementArrayFinder in protractor based on the text of getText()的后续问题

因此,根据我给出的答案,我使用了.filter() 方法。它找到了我想要的行,但我得到了

Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By(css selector, li)

所以,我在其中添加了一些登录信息。这是我的代码:

function getCardByName(name) {
    let cards = element.all(by.css(li));
    cards.count().then(c => console.log("The count is ", c));
    let theCard = cards.filter(function(el, i) {
        return el.getText().then(function(text) {
            text = text.substr(0, text.search(" ID:"));
            console.log(i + ") Does the text, " + text + ", match name, " + name + "?  " + (text == name) + " | " + (text === name));
            text == name
        });
     }).first();

     return new Card(theCard);
}

此代码将选择并返回第一个 ElementFinder 对象,并将其作为参数传递给 Card 对象。

我在实际页面上得到的结果是(名称不是真实的):

The count is  7
0) Does text, KERGER, FEYSAL, equal name, CRAVEN, LILLARD?  false | false
1) Does text, JENNINGS, JEWELLAN, equal name, CRAVEN, LILLARD?  false | false
2) Does text, CRAVEN, LILLARD, equal name, CRAVEN, LILLARD?  true | true
3) Does text, HORSTMANN, GREG, equal name, CRAVEN, LILLARD?  false | false
4) Does text, MEUSA, FRANKLIN, equal name, CRAVEN, LILLARD?  false | false
5) Does text, LAURITO, RANDOLPH, equal name, CRAVEN, LILLARD?  false | false
6) Does text, JHANSON, LORENE, equal name, CRAVEN, LILLARD?  false | false
0) Does text, KERGER, FEYSAL, equal name, CRAVEN, LILLARD?  false | false
1) Does text, JENNINGS, JEWELLAN, equal name, CRAVEN, LILLARD?  false | false
2) Does text, CRAVEN, LILLARD, equal name, CRAVEN, LILLARD?  true | true
3) Does text, HORSTMANN, GREG, equal name, CRAVEN, LILLARD?  false | false
4) Does text, MEUSA, FRANKLIN, equal name, CRAVEN, LILLARD?  false | false
5) Does text, LAURITO, RANDOLPH, equal name, CRAVEN, LILLARD?  false | false
6) Does text, JHANSON, LORENE, equal name, CRAVEN, LILLARD?  false | false
0) Does text, KERGER, FEYSAL, equal name, CRAVEN, LILLARD?  false | false
1) Does text, JENNINGS, JEWELLAN, equal name, CRAVEN, LILLARD?  false | false
2) Does text, CRAVEN, LILLARD, equal name, CRAVEN, LILLARD?  true | true
3) Does text, HORSTMANN, GREG, equal name, CRAVEN, LILLARD?  false | false
...


Failed: Index out of bound. ...

这继续循环 6 行 13 次(使用我实际测试的页面)。但它确实找到了我想要找到的行(见结果的第 3 行的“true | true”?)为什么它循环了 13 次并且没有停止并且没有将匹配的结果加载到结果数组中?

【问题讨论】:

    标签: protractor


    【解决方案1】:

    据我所见,您实际上并没有返回布尔值(如果这是您代码中的复制粘贴)。您要做的是(注意“返回文本 == 名称”):

    function getCardByName(name) {
        let cards = element.all(by.css(li));
        cards.count().then(c => console.log("The count is ", c));
        let theCard = cards.filter(function(el, i) {
            return el.getText().then(function(text) {
                text = text.substr(0, text.search(" ID:"));
                console.log(i + ") Does the text, " + text + ", match name, " + name + "?  " + (text == name) + " | " + (text === name));
                return text == name;
            });
         }).first();
    
         return new Card(theCard);
    }
    

    【讨论】:

    • 不幸的是,我只能给出与我正在做的类似的示例代码。但是我没有将“返回”与比较器放在一起。 ...这就是我所缺少的!谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-05-22
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多