WebdriverIO从x页面中抓取内容

时间:2016-07-30 06:43:17

标签: javascript selenium webdriver-io

所以我正在使用WebdriverIO开发一个API,以便为没有API支持的网站自动化一些东西。目前面临尝试动态刮取x个页面的问题。该场景只说每页上列出了20个项目,我可以单击下一步跳转到下一个,抓取并重复直到没有更多页面。

问题;没有特定数量的页面,一次可能是2,另一个可能是12,所以我需要动态地执行此操作。

我的问题(我认为);从控制台输出来看,我认为selenium会话在递归抓取完成之前就已经被杀死了,那是因为它正在进行执行,我在那里进行测试。

我的问题;如何在没有脚本继续的情况下递归抓取这些页面并在完成之前终止会话?

这是我到目前为止的略有编辑版本。谢谢!

...webdriver doing stuff that works...
.then(function() {
  console.log('Entering the initial THIS...');

  function recursive() {
    console.log('This is inside the RECURSIVE Function!');

    client
      .execute(function() {
        return document.title;
      })
      .then(function(ret) {
        console.log('The thing I asked it for is: ' + ret.value);
      })
      .isExisting('#lastPage').then(function(lastPage) {
        console.log(lastPage);

        if (!lastPage) {
          console.log('Its not the last page!');
          client
            .click('//a[text()="Next"]')
            .saveScreenshot('/src/api/debug_images/05.png')

          recursive();
        }
      })
  }

  recursive();
})
.execute(function() {
  var table = document.getElementById("transactionHistoryTable");
  var result = [];

  for (var i = 1, row; row = table.rows[i]; i++) {
    result.push({
      date: row.cells[0].innerText,
      details: row.cells[1].innerText,
      debt: row.cells[2].innerText,
      credit: row.cells[3].innerText,
      balance: row.cells[4].innerText
    });
  }

  return result;
})
.then(function(ret) {
  return ret.value;
})
.then(function(ledger) {
  console.log('This should not run yet?');
  res.json({
    status: 'success',
    request: { bsb: bsb, account: account, period: period },
    ledger: ledger.length
  });
})
.end();

0 个答案:

没有答案