2016-10-04 187 views
1

我試圖做一些與nightmare刮,我的工作幾乎功能。問題是我在調用evaluate()run()後嘗試執行click()時遇到了問題。在我運行這兩個函數後,我嘗試再次點擊以將自己移動到網站的另一部分,但未執行click()點擊功能不執行執行Nightmare.js

在這一點上,我注意到肯定什麼問題,我有幾個假設,也許,這些功能是異步的,我想click()當回調的arent準備好了沒有,或上述功能中的一個結束德電流nightmare對象,我沒有範圍了。

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

我是有異步回調的問題,所以我做了什麼,是我嵌套惡夢對象的調用,以確保任務正在運行一個另一個之後。這是代碼:

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!'); 


});