【问题标题】:Looping elements in WebdriverIOWebdriverIO 中的循环元素
【发布时间】:2016-01-20 20:05:47
【问题描述】:

我正在尝试遍历链接列表,并对每个链接执行一些操作。我可以使用elements 迭代元素,但是在forEach 中使用click 不会阻止forEach 中的下一步,而且Selenium 发疯了,因为它试图继续对不在DOM 中的元素执行操作。

var q = require("q");
var webdriverio = require('webdriverio');
var options = {
    desiredCapabilities: {
        browserName: 'chrome'
    }
};

var clicks = [];

var runner = webdriverio.remote(options);

runner
    .init()
    .url('https://www.google.dk/search?q=burrito')
    .elements(".r").then(function(res){
        res.value.forEach(function(elem){
            console.log(elem);
            clicks.push(
                runner
                    .elementIdClick(elem.ELEMENT)
                    .pause(5000)
                    .back()
                    .pause(2000)
            );
        });

        return q.all(clicks);
    });

如何确保在 forEach 中执行所有代码之前不会运行 forEach 中的下一次迭代?

编辑:我应该提到我已经尝试过https://github.com/webdriverio/webdriverio/issues/941https://github.com/webdriverio/webdriverio/issues/273。我用更具体的内容更新了我的代码示例。

【问题讨论】:

    标签: node.js selenium selenium-webdriver webdriver-io


    【解决方案1】:
    var runner = webdriverjs
    .remote(options)
    .init()
    .url("http://www.google.com")
    
     // fetch elements
    .elements('a', function(err, res){
        // iterate through elements
        res.value.forEach(function(elem) {
            // execute specific action
            runner.elementIdClick(elem.Element, function(err, res) {
                 // callback logic here
                 // ...
            })
        })
    })
    

    来自https://github.com/webdriverio/webdriverio/issues/273

    【讨论】:

    • 我应该提到我已经尝试过我可以在 github 上找到的解决方案。我更新了我的问题。对不起。
    【解决方案2】:

    根据 WebdriverIO 的创建者this response,循环访问某些链接并单击它们的正确方法:

    runner
        .init()
        .url('https://www.google.dk/search?q=burrito')
        .getText(".r").then(function(res){
            console.log(res);
            res.forEach(function(elem){
                console.log(elem);
                clicks.push(
                    runner
                        .click('=' + elem)
                        .back()
                );
            });
    
            return q.all(clicks);
        });
    

    【讨论】:

      【解决方案3】:

      您可能希望将 Async 包用于类似的事情。

      http://caolan.github.io/async/

      async.eachSeries(hugeArray, function iteratee(item, callback) {
          if (inCache(item)) {
              callback(null, cache[item]); // if many items are cached, you'll overflow
          } else {
              doSomeIO(item, callback);
          }
      }, function done() {
          //...
      });
      

      【讨论】:

        【解决方案4】:

        从元素列表中提取文本的简单方法

        I am able to extract text for the list of webelements and then store it to the Array .
        
        
        var strColumntext = [] ;
            var table =browser.$(".//*/table/tbody");
        
           table.$$(".//tr/th").map(function(element){
                 console.log("Extracted text is : "+element.getText());
                 strColumntext.push(element.getText());
            });
            
            console.log("Print the all texts : "+strColumntext)

        You can see Youtube Demo for this

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多