2016-10-04 191 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!'); 


});