【问题标题】:Nightmare Loop Through Click Events噩梦循环通过点击事件
【发布时间】:2017-03-04 00:39:02
【问题描述】:

我正在学习噩梦。尝试访问一个网站,登录,然后反复单击一个按钮,以使更多数据出现,直到该按钮不再存在。我做了一个虚拟帐户来说明。

我第一次成功登录并单击该按钮,但是当我尝试再次单击它时,它会记录一个错误,即找不到“.more-checkins”元素。

最终,我希望这段代码处于循环中,而不是让代码单击...等待...然后再次单击。帮助设计也将不胜感激。

const Nightmare = require('nightmare')
const untappdURL = 'https://untappd.com/user/beerFan2017'


Nightmare({
    show: true,
    openDevTools: true,
    waitTimeout: 90000000 // increase the default timeout to test things
})
    .goto(untappdURL)
    .click('.more_checkins')
    .type('#username', 'beerFan2017')
    .type('#password', 'Testing2017')
    .click('input[type="submit"]')
    .wait('.stats')
    .click('.more_checkins')
    .evaluate(function() {
        return console.log('Begin waiting');
    })
    .wait(5000)
    .evaluate(function() {
        return console.log('Waiting end');
    })
    .click('more_checkins')
    .then(result => console.log(result))
    .catch(error => console.error(error))

【问题讨论】:

    标签: javascript web-scraping phantomjs nightmare


    【解决方案1】:

    两部分答案:一,去阅读asynchronous operations and loopsnightmare-examples。这将帮助您了解有关使用 Promise 进行迭代的背景知识。

    第二,Nightmare repo 中有一个类似的问题 - #625 - 当您到达滚动条的末尾时,它与继续加载更多内容有关。一个示例实现(即非常天真,小心):

    var Nightmare = require('nightmare');
    var vo = require('vo');
    var nightmare = Nightmare({
      show: true
    });
    
    var run = function * () {
      yield nightmare.goto('http://someInfiniteScrollPage.tld');
    
      var previousHeight, currentHeight=0;
      while(previousHeight !== currentHeight) {
        previousHeight = currentHeight;
        var currentHeight = yield nightmare.evaluate(function() {
          return document.body.scrollHeight;
        });
        yield nightmare.scrollTo(currentHeight, 0)
          .wait(3000);
      }
      yield nightmare.end();
    };
    
    vo(run)(function(err) {
      console.dir(err);
      console.log('done');
    });
    

    希望这足以让您开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多