Nightmare conditional wait()

后端 未结 3 1505
执笔经年
执笔经年 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:14

    Here i create a function to get html sources for different conditions, i'm crawling the TimeWarnerCable page to get info about TV, Internet and Bundle plans, so my function get some parameters and react for each one in different calls. You can use the .exists() to check selectors and then continue with nightmare

    function getSource(url,serviceQuantity,zip){
      var defer=Q.defer();
      var Nightmare = require('nightmare');
      var nightmare = Nightmare({openDevTools:browserDev ,show: browserVisible,'webPreferences':{partition: 'nopersist'}});
    
      nightmare
      .goto(url)
      .cookies.clear()
      .wait(2000)
      .exists('div.messagebox-wrapper.twc-container[style="display: block;"]')
      .then(function(noZipSet){
        if (noZipSet){
          debug('No zipcode settled down');
          nightmare
          .insert('fieldset > div > input[placeholder="Enter Your ZIP Code"]',zip)
          .type('fieldset > div > input[placeholder="Enter Your ZIP Code"]', '\u000d');//I do "Enter" because nightmare can't find the submit button
        }else{
          debug('Zipcode settled down');
          nightmare
          .click('div.section.newHeaderIcons > div > ul > li:nth-child(4) > div > a')
          .wait(2000)
          .insert('form.geoLoc > fieldset > div > input[placeholder="Update Your ZIP Code"]',zip)
          .type('form.geoLoc > fieldset > div > input[placeholder="Update Your ZIP Code"]', '\u000d');//I do "Enter" because nightmare can't find the submit button
        }
        nightmare
        .wait(8500)
        .exists('div[style="display: block;"] > div > div > div > div > div > div > div.parsys.oof-error-content > div > div > div > div > div > div > p[style="color: #333333;"]')
        .then(function(zipNotAvailable){
          if (zipNotAvailable){
            debug('Service not available in '+zip+' for '+serviceQuantity+' services');
            nightmare
              .end()
              .then(function(){
                defer.resolve('');
              });
          }else{
            debug('Service available on the zipcode');
          switch (serviceQuantity) {
              case 1:
                  nightmare
                      .evaluate(function(){
                        return document.querySelector('html').innerHTML;
                      })
                      .end()
                      .then(function (result) {
                        defer.resolve(result);
                      })
                      .catch(function (error) {
                        debug('ERROR >> Search failed:', error);
                      });
                  break;
              case 2:
                  nightmare
                    .click('#tv-filter')
                    .wait(500)
                    .click('#internet-filter')
                    .wait(500)
                    .evaluate(function(){
                      return document.querySelector('html').innerHTML;
                    })
                    .end()
                    .then(function (result) {
                       defer.resolve(result);
                    })
                    .catch(function (error) {
                       debug('ERROR >> Search failed:', error);
                    });
                  break;
              case 3:
                  nightmare
                      .click('#tv-filter')
                      .wait(500)
                      .click('#internet-filter')
                      .wait(500)
                      .click('#phone-filter')
                      .wait(500)
                      .evaluate(function(){
                        return document.querySelector('html').innerHTML;
                      })
                      .end()
                      .then(function (result) {
                        defer.resolve(result);
                      })
                      .catch(function (error) {
                        debug('ERROR >> Search failed:', error);
                      });
                      break;
           }
          }
        });
      });
      return defer.promise;
    }
    

提交回复
热议问题