2016-10-04 6 views
1

私はnightmareでいくつかの削り取りをしようとしています。私の仕事はほとんど機能しています。問題は、evaluate()run()が呼び出された後にclick()を実行しようとすると問題が発生することです。私はこれらの2つの機能を実行した後、別のクリックをして自分自身をウェブサイトの別の部分に移動しようとしていますが、click()を実行していません。問題は、私は、いくつかの仮定を持っているかもしれないそれらの機能は非同期であり、コールバックの準備ができアレントまだまたはそれらの機能の一つは、デ現在nightmareオブジェクトを終了したとき、私はclick()にしようとしているいただきました!私はノート確信している。この時点でクリック機能が実行されていませんNightmare.js

と私はもうスコープを持っていません。

var Nightmare = require('nightmare'); 
//var nightmare = Nightmare({show:true}) 
var express = require('express'); 
var fs = require('fs'); 
var request = require('request'); 
var cheerio = require('cheerio'); 
var app  = express(); 

var urlWeb = "someurl"; 
var selectCity = "#ddl_city"; 
var selectTheater = "#ddl_theater"; 
var enterBtn = "#btn_enter"; 
var mainSelector = "#aspnetForm"; 
var flagReady = true; 

new Nightmare({show:true}) 
.goto(urlWeb) 
.wait(selectCity) 
.select(selectCity, '19') 
.wait(8000) 
.select(selectTheater, '12') 
.wait(1000) 
.click(enterBtn) 
.wait(mainSelector) 
.evaluate(function(){ 

     //returning HTML for cheerio 
     return document.body.innerHTML; 
}) 
.run(function(err, nightmare){ 
    if (err) return console.log(err); 

    // Loading HTML body on jquery cheerio 
    var $ = cheerio.load(nightmare); 

    //Looping on each div for seccion de Carterla para Hoy 
    $('.showtimeDaily').each(function(index, element){ 
     //spanish title 
     console.log($(this).find('h3').children().text()); 
     //english title 
     console.log($(this).find('h4').text()); 
     //schedule for today 
     console.log($(this).find('li').children().text() + " "); 
     //img for movie 
     console.log($(this).find('img').attr('src')); 
      //show time data such as gender, lenght, language 
     console.log($(this).find('.showtimeData').text()); 
     var showtimeData = $(this).find('.showtimeData').text(); 
     //console.log(JSON.stringify(showtimeData.replace(/\t|\n/g, ""))); 
    }) 

     console.log('Done!'); 


}) 
//*****here is wen I try to click***** 
.click('a[href="../showtimes/weekly.aspx"]'); 

答えて

1

私は、非同期コールバックの問題を持つので、私がやったことは、私はタスクが他の1の後に1を実行していたことを確認するために悪夢オブジェクトの呼び出しを入れ子になったということですました。これはコードです:

nightmare 
.goto(urlWeb) 
.wait(selectCity) 
.select(selectCity, '19') 
.wait(8000) 
.select(selectTheater, '12') 
.wait(1000) 
.click(enterBtn) 
.wait(mainSelector) 
.evaluate(function(){ 

     //returning HTML for cheerio 
     return document.body.innerHTML; 
}) 
.then(function(body){ 
    // Loading HTML body on jquery cheerio 
    var $ = cheerio.load(body); 

    //Looping on each div for seccion de Carterla para Hoy 
    $('.showtimeDaily').each(function(index, element){ 
     //spanish title 
     console.log($(this).find('h3').children().text()); 
     //english title 
     console.log($(this).find('h4').text()); 
     //schedule for today 
     console.log($(this).find('li').children().text() + " "); 
     //img for movie 
     console.log($(this).find('img').attr('src')); 
      //show time data such as gender, lenght, language 
     console.log($(this).find('.showtimeData').text()); 
     var showtimeData = $(this).find('.showtimeData').text(); 
     //console.log(JSON.stringify(showtimeData.replace(/\t|\n/g, ""))); 
    }) 
    //**Here I call nightmare to run after the first call back is done***** 
    nightmare 
     .goto('') 
     .wait('body') 
     .title() 
     .then(function(title){ 
      console.log(title); 
     }); 

     console.log('Done!'); 


}); 
関連する問題