Intern: loop on Promise.<Array.<leadfoot/Element>>

回眸只為那壹抹淺笑 提交于 2019-12-05 22:51:33

The simplest way to do this would be to use an Xpath expression to select the item directly, like:

.findByXpath('//div[@class="child" and div[@class="label" and text()="B"]]/div[@class="ico"]')

The expression above will find the first div with class "ico" that's the child of a div with class "child" that has a child div with class "label" and text content "B".


Update

Using an Xpath expression is almost always preferable to looping through elements using Leadfoot commands because it's significantly more efficient, but if looping is desired for some reason, you can do something like:

var my_label = null;
this.remote
    .findAllByCssSelector('.my-selector')
    .then(function (elementArray) {
        return Promise.all(elementArray.map(function (element) {
            return element.getVisibleText()
                .then(function (text) {
                    if (text === my_label) {
                        return element.findByCssSelector('.ico')
                            .then(function (ico) {
                                return ico.click();
                            });
                    }
                });
        });
    });

A couple of key points to note:

  1. You need to return Promises/Commands from then callbacks when you're performing async operations in the then callbacks
  2. Element methods (like element.findByCssSelector) return Promises, not Commands, so you can't call click on the result.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!