title: Javascript Loop over elements and click a link using WebdriverIO
body: <p>I am using Javascript, webdriverio (v2.1.2) to perform some data extraction from an internal site. So the idea is</p>
  
  <ul>
  <li>Authenticate</li>
  <li>Open the required URL, when authenticated</li>
  <li>In the new page, search for an anchor tag having specific keyword</li>
  <li>Once found, click on the anchor tag</li>
  </ul>
  
  <p>Below is what I have tried and it works (last two points). I had to use Q and async to achieve it. I was hoping to use only Q to achieve it. <strong>Can someone help me, on how to achieve it using Q only ??</strong></p>
  
  <pre><code>    var EmployeeAllocationDetails = (function () {
  'use stricy';
  /*jslint nomen: true */
  var Q = require('Q'),
      async = require('async'),   
      _ead_name = 'Employee Allocation Details',
      goToEadFromHome;
  
  goToEadFromHome = function (browserClient) {
      browserClient.pause(500);
      var deferred = Q.defer();
      browserClient.elements('table.rmg td.workListTD div.tab2 div.contentDiv&gt;a', function (err, results) {
          if (err) {
              deferred.reject(new Error('Unable to get EAD page. ' + JSON.stringify(err)));
          } else {
  
          async.each(results.value, function (oneResult, callback) {
                  console.log('Processing: ' + JSON.stringify(oneResult));
                  browserClient.elementIdText(oneResult.ELEMENT, function (err, result) {
                      if (err) {
                          if (err.message.indexOf('referenced element is no longer attached to the DOM') &gt; -1 ){
                              callback();
                          } else {
                              callback('Error while processing :' + JSON.stringify(oneResult) + '. ' + err);
                          }
                      } else if(!result){
                          console.log('result undefined. Cannot process: ' + JSON.stringify(oneResult));
                          callback();
                      } else if(result.value.trim() === _ead_name){
                          deferred.resolve(oneResult);
                          callback();
                      }
                  });
              }, function (err) {
                  // if any of the processing produced an error, err would equal that error
              if( err ) {
          // One of the iterations produced an error.
          // All processing will now stop.
              console.log('A processing failed to process. ' + err);
              } else {
              console.log('All results have been processed successfully');
              }
              }); //end of async.each
  
          }
      });
      return deferred.promise;
  };
  
  return {
      launchEad : goToEadFromHome
    }
  })();
  module.exports = EmployeeAllocationDetails;
  </code></pre>
  
  <p>Related Github Issue link <a href="https://github.com/webdriverio/webdriverio/issues/123" rel="nofollow">https://github.com/webdriverio/webdriverio/issues/123</a></p>