It is additional question of How to stop a loop when clicking asynchronously in CasperJS
I tried this code
function execOnce(casper, i, max){
// end condition
if (i === max) {
return;
}
casper.wait(3000, function() {
var button = x('//*[@id="content"]/div[3]/a['+i+']');
if (!this.exists(button)) {
this.echo(i + " not available");
return;
}
this.thenClick(button, function (){
console.log('Searching dic');
words = words.concat(this.evaluate(getWords));
// recursive step
execOnce(this, i+1, max);
});
});
};
// start the recursive chain
casper.then(function(){
execOnce(this, 1, 200);
});
But I found that indexes' Xpath of my target web pages has iteration.
When it reached '//*[@id="mArticle"]/div[2]/a['11']'
next index's Xpath becomes '//*[@id="mArticle"]/div[2]/a['2']
(back to a['2'])
for example the webpage url is "http://krdic.naver.com/search.nhn?query=%E3%85%8F%E3%85%8F&kind=keyword"
under the page there are [1][2][3][4][5][6][7][8][9][10] [Next Page]
When I click Next page you can see
[Previous Page][11][12][13][14][15][16][17][18][19][20] [Next Page]
but [12] 's Xpath is not //*[@id="content"]/div[3]/a[12]
---> It is
//*[@id="content"]/div[3]/a[2]
So I have to do iteration of function execOnce
including code casper.wait(6000, function() {}
because my target website is really sensitive to query so I put "wait" code whenever I can..!
In case of this can I use nested function like this?
function execOnce(casper, i, max){
if (i === max) {
function execOnce(casper, i, max){
return;
}
...