내 스크립트는 단순히 페이지의 보고서 수를 수집 한 다음 다음 페이지로 이동하여 동일한 작업을 수행합니다. 목표는 여러 페이지에 걸쳐 총 보고서 수를 얻는 것입니다. 웹 사이트의 여러 페이지 구문 분석 및 총 개수 계산
여기
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
안녕하세요. 귀하의 회신에 감사드립니다. 필자는 공개 된 코드와 분리 된 코드를 수정했다. 나는 .thenOpen 함수 밖에서 새로운 카운트 값을 얻는 방법을 아직 알 수 없다. newReportCount의 로그는 값이 반환되기 전에 실행 된 것처럼 보이므로 개체를 얻는 이유는 무엇입니까? 일종의 콜백이 필요할 것 같지만 어떻게 캐스퍼에 구현할 수 있을까요? – JTu
@JeffreyTu 제 답변을주의 깊게 읽으십시오. 다른 모든'then *'과'wait *'함수처럼 비동기이기 때문에'casper.thenOpen'에서 어떤 것을 반환 할 수 없습니다. 아마도 'newCount'가 전역 변수가되어 추가 할 수 있을까요? –