2015-02-07 1 views
0

내 스크립트는 단순히 페이지의 보고서 수를 수집 한 다음 다음 페이지로 이동하여 동일한 작업을 수행합니다. 목표는 여러 페이지에 걸쳐 총 보고서 수를 얻는 것입니다. 웹 사이트의 여러 페이지 구문 분석 및 총 개수 계산

여기

var casper = require('casper').create({ 
    clientScripts: ["./lib/jquery-2.1.3.min.js"], 
    // verbose: true, 
    logLevel: "debug" 
}); 

casper.on('remote.message', function(msg) { 
    this.echo('LOG: ' + msg); 
}); 

casper.on('page.error', function (msg, trace) { 
    this.echo('Error: ' + msg, 'ERROR'); 
}); 

var reportCount, newReportCount, reportPages; 

casper.start("reports.html", function() { 

    reportPages = this.evaluate(function() { 
     return $('#table2 tbody tr td').children('a').length -1; 
    }); 

    //first page of reports 
    reportCount = this.evaluate(function() { 
     return $('#table1 tbody').first().children('tr').length; 
    }); 

    this.echo('initial count: ' + reportCount); 
    this.echo('pages: ' + reportPages); 

    //check if more than 1 page and add report count 
    if (reportPages > 1) { 
    newReportCount = this.thenOpen('reports2.html', function(){ 
     var newCount = this.evaluate(function(count) { 
      add = count + $('#table1 tbody').first().children('tr').length; 
      // console.log('new count inside: ' + add); 
      return add; 
     }, reportCount); 
     console.log(newCount); //this shows correct new value 32 
    }); 
    console.log(newReportCount); //this shows [object Casper] 

    neoReportCount = this.thenOpen('reports3.html', function(count){ 
     console.log(newReportCount); //this shows [object Casper] 
     //do the same count 
    }, newReportCount); 
    } 

casper.run(); 

업데이트 콘솔

 
Pages: 3 
First count: 15 
[object Casper], currently at file:///**/reports.html 
32 
[object Casper], currently at file:///**/reports3.html 

답변

0

네, 가능의 출력이지만 그 안에 단어 then을 가지고 casper.thenOpenAndEvaluate()를 사용합니다. 즉,이 함수는 비동기 적이며 builder/promise 패턴을 사용하기 위해 casper 객체를 반환합니다. 그래서 당신은 이와 같은 함수로부터 아무것도 반환 할 수 없습니다. 비동기 적이므로 현재 단계가 끝난 후 실행되며 console.log(newCount); 이후입니다.

당신은 다음과 같은 예를 들어, 기능을 분리해야합니다 : 여러 페이지에 걸쳐 반복하고 싶지처럼

//check if more than 1 page and add report count 
if (reportPages > 1) { 
    var newCount; 
    this.thenOpen('reports2.html', function(count){ 
    newCount = this.evaluate(function(count){ 
     add = count + $('#table1 tbody').first().children('tr').length; 
     console.log('new count inside: ' + add); 
     return add; 
    }, reportCount); 
    console.log(newCount); 
    }).thenOpen('reports3.html', function(count){ 
    newCount += this.evaluate(function(count){ 
     add = count + $('#table1 tbody').first().children('tr').length; 
     console.log('new count inside: ' + add); 
     return add; 
    }, reportCount); 
    console.log(newCount); 
    }).then(function(){ 
    console.log(newCount); 
    }); 
} 

것 같다. 이것은 대개 CasperJS가 비동기 적이며 열어야하는 페이지의 수를 미리 알지 못하기 때문에 재귀 적으로 수행됩니다. 몇 가지 예를 들어이 질문을 살펴 보시기 바랍니다. CasperJS loop or iterate through multiple web pages?

+0

안녕하세요. 귀하의 회신에 감사드립니다. 필자는 공개 된 코드와 분리 된 코드를 수정했다. 나는 .thenOpen 함수 밖에서 새로운 카운트 값을 얻는 방법을 아직 알 수 없다. newReportCount의 로그는 값이 반환되기 전에 실행 된 것처럼 보이므로 개체를 얻는 이유는 무엇입니까? 일종의 콜백이 필요할 것 같지만 어떻게 캐스퍼에 구현할 수 있을까요? – JTu

+0

@JeffreyTu 제 답변을주의 깊게 읽으십시오. 다른 모든'then *'과'wait *'함수처럼 비동기이기 때문에'casper.thenOpen'에서 어떤 것을 반환 할 수 없습니다. 아마도 'newCount'가 전역 변수가되어 추가 할 수 있을까요? –

관련 문제