【问题标题】:getElement by id and loop through all elements inside in nightwatchgetElement by id 并循环遍历 nightwatch 中的所有元素
【发布时间】:2015-12-07 14:45:06
【问题描述】:
<div id="productcontainer" class="products-list" style="display: block;">
<a id="prd_Item_0" class="item-list-300x250">links</a>
<a id="prd_Item_1" class="item-list-300x250">links</a>
<a id="prd_Item_1" class="item-list-300x250">links</a>
<a id="prd_Item_1" class="item-list-300x250">links</a></div>

var ele=document.getElementById("productcontainer");
if(ele)
  {
    tags=ele.getElementsByTagName("a");
    for(i=0;i<tags.length;i++)
     {
      if(tags[i])
        {
         tags[i].click();
        }
     }
}

我想在 nightwatch js 中像上面那样做,谁能帮助我如何使用 nightwtch js 像上面那样做。

提前致谢

【问题讨论】:

  • 大家帮我看看

标签: selenium nightwatch.js


【解决方案1】:

使用元素查找器findElements 返回的promise,然后循环遍历锚元素以执行您想要的任何操作。试试下面的代码 -

driver.findElements(By.cssSelector('#productcontainer a')).then(function(tags){
    for (var i = 0; i< tags.length; i++){
        if(tags[i])
            tags[i].click();
    }
});

希望这会有所帮助。

【讨论】:

  • module.exports = { 'Demo test NightwatchJS.org' : function (client) { client .url('test.com/') .waitForElementVisible('body', 1000) .elements('css 选择器', '#productcontainer', function (result) { for (var i = 0; i
  • @Chethu 你能试试这个吗?module.exports = { 'Demo test NightwatchJS.org' : function (client) { client .url('test.com/') .waitForElementVisible('body', 1000) .elements('css selector', '#productcontainer a', function (result) { for (var i = 0; i &lt; result.value.length; i++) { console.log(client) } }) .end() } }; 只需将你的定位器从#productcontainer 更改为#productcontainer a
【解决方案2】:

这应该做的工作..

module.exports = {
  'Iterate over elements and click them': function(browser) {

    function iterate(elements) {
      elements.value.forEach(function(el) {
        browser.click(el.ELEMENT, function(r) {
          browser.assert.ok(r.status === 0);
        });
      });
    }

    browser
      .url('..')
      .elements('css selector', 'div#productcontainer a', iterate)
      .end();
  }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多