Nightmare conditional wait()

后端 未结 3 1523
执笔经年
执笔经年 2020-12-20 05:24

I\'m trying to crawl a webpage using Nightmare, but want to wait for #someelem to be present, only if it actually exists. Otherwise, I want Nightmare to move on

3条回答
  •  渐次进展
    2020-12-20 06:17

    I don't think passing the cheerio library as you have it is going to work very well. The arguments get serialized (more or less) to be passed to the child Electron process, so passing an entire library probably won't work.

    On the up side, the fn part of .wait(fn) is executed in the page context - meaning you have full access to document and the methods it has (eg, querySelector). You could also have access to the page's jQuery context if it exists, or you could even use .inject() to inject it if not.

    Setting that aside, you're right insofar as .wait() (and .evaluate(), for that matter) expect a synchronous method, at least until something like promises could be used directly in .evaluate().

    Until that is available, you could use .action() to mimic the behavior you want:

    var Nightmare = require('nightmare');
    
    Nightmare.action('deferredWait', function(done) {
      var attempt = 0;
      var self = this;
    
      function doEval() {
        self.evaluate_now(function(selector) {
          return (document.querySelector(selector) !== null);
        }, function(result) {
          if (result) {
            done(null, true);
          } else {
            attempt++;
            if (attempt < 10) {
              setTimeout(doEval, 2000); //This seems iffy.
            } else {
              done(null, false);
            }
          }
        }, '#elem');
      };
      doEval();
      return this;
    });
    
    var nightmare = Nightmare();
    nightmare.goto('http://example.com')
      .deferredWait()
      .then(function(result) {
        console.log(result);
      });
    

提交回复
热议问题