2016-07-10 2 views
1

저는 CasperJS 초보자입니다.for 루프에서 클릭 이벤트 | i 값은 항상 마지막 값입니다.

저는 모든 ul을 반복하고 각 li을 클릭하고 싶습니다. li을 클릭하면 모달이 팝업되고 모달 데이터가 저장됩니다. 그러나 루프 내부의 'i'값은 항상 최종 값입니다.
ul에 5 개의 lis가 있습니다.
다음 루프는 항상 5th li을 5 회 클릭하고 5th li을 클릭하면 5 회 모달 데이터를 저장합니다.

casper.then(function() { 
    a = lis.length; 
    this.echo(a + ' lis found'); 
    for(var i = 1; i <= a; i++) { 
    this.echo(i + ' now'); 
    this.click('.hello:nth-child('+ i +')'); 
    casper.waitUntilVisible('.modal__content ', function() { 
     console.log('Open Modal'); 
     links = links.concat(this.evaluate(getLinks)); 
    }); 
    } 
}); 

Google 검색 결과에서 이벤트 리스너의 지정을 마감 처리해야한다고 알았습니다. 그러나 이것은 아무 것도 울리지 않습니다. thenClick에 클릭 변경

casper.then(function() { 
    a = lis.length; 
    this.echo(a + ' lis found'); 
    for(var i = 1; i <= a; i++) { 
    (function(i){ // Added this line 
     this.echo(i + ' now'); 
     this.click('.hello:nth-child('+ i +')'); 
     casper.waitUntilVisible('.modal__content ', function() { 
     console.log('Open Modal'); 
     links = links.concat(this.evaluate(getLinks)); 
     }); 
    })(i); // Added this line 
    } 
}); 

답변

1

문제 :

casper.then(function() { 
    a = lis.length; 
    this.echo(a + ' lis found'); 
    for(var i = 1; i <= a; i++) { 
    this.echo(i + ' now'); 
    this.thenClick('.hello:nth-child('+ i +')'); // Changed to thenClick from click 
    casper.waitUntilVisible('.modal__content ', function() { 
     console.log('Open Modal'); 
     links = links.concat(this.evaluate(getLinks)); 
    }); 
    } 
}); 
해결