javascript - 执行 ElementArrayFinder

标签 javascript angularjs selenium selenium-webdriver protractor

我正在学习 Protractor ,到目前为止这是一段疯狂的旅程,因为我对 Javascript 也很陌生。到目前为止,我了解到 Protractor 对所有 promise 进行排队,并且可以使用 then() 执行它们。 但是,我现在正尝试在 ElementArrayFinder 上使用 filter() 但它似乎没有执行。只有当我在它前面加上 return-keyword 时,过滤器才会被执行,但随后我离开了我的函数,我不想那样做。 有人可以帮助我理解这一点吗?

在我的代码下面:

it('Select service', function() {
 servicePage.services.filter(function(elem, index) {
      return elem.getAttribute('class').then(function(attribute) {
          console.log('*****' + attribute);
          return attribute === 'service passive';
      });
  });
servicePage.services.get(0).element(by.tagName('input')).click(); 
});

在上面运行时,没有执行控制台日志,所以我猜过滤器功能没有被执行。当我像下面那样执行时,会执行过滤器,但不会执行 click()。

it('Select service', function() {
 return servicePage.services.filter(function(elem, index) {
      return elem.getAttribute('class').then(function(attribute) {
          console.log('*****' + attribute);
          return attribute === 'service passive';
      });
  });
servicePage.services.get(0).element(by.tagName('input')).click(); 
});

示例 3:

    it('Select service', function() {
      servicePage.services.filter(function(elem, index) {
        return elem.getAttribute('class').then(function(attribute) {
          console.log('*****' + attribute);
          return attribute === 'service passive';
      });
  }).first().element(by.tagName('input')).click();
});

提前致谢! 问候

最佳答案

您应该捕获过滤器函数返回的元素,然后对其执行操作。 filter()函数返回与您在函数中指定的条件匹配的元素。在您的情况下,它返回一个具有 class 属性 service passive 的元素。如果有多个具有相同属性的元素,那么您可能必须链接 get()对所需元素执行操作的函数。方法如下 -

servicePage.services.filter(function(elem, index) {
      return elem.getAttribute('class').then(function(attribute) {
          console.log('*****' + attribute);
          return attribute === 'service passive';
      });
}).element(by.tagName('input')).click(); //if only one element is returning, click on it

或者当有更多符合条件的元素时,将最后一行替换为下一行-

}).get(1).element(by.tagName('input')).click(); //click the second element in the array of elements returned

希望对您有所帮助。

关于javascript - 执行 ElementArrayFinder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35313009/

相关文章:

javascript - Firebase 从函数返回输出

javascript - 更改默认的 Google Maps Api 默认缩放控件图像?

javascript - 一些所见即所得的编辑器如何保持粘贴文本的格式?

android - ui-router 保持 Controller Activity

php - 在编辑原始图像的同时裁剪图像的小版本

angularjs - Angular JS : Detect if ng-bind-html finished loading then highlight code syntax

javascript - angularjs firebase storage getDownloadURL 返回未定义,但它存在

testing - 碲和 Selenium 的区别2

javascript - 如何创建javascript执行器以使元素在selenium webdriver中可见

javascript - Python selenium 如何使用参数在 JavaScript 元素上执行execute_script