0

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]

enter image description here

When I click Next page you can see

 [Previous Page][11][12][13][14][15][16][17][18][19][20] [Next Page]

enter image description here

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;
        }
        ...
Community
  • 1
  • 1
Hyung Kyu Park
  • 255
  • 2
  • 3
  • 12

1 Answers1

1

XPath is very expressive. You can for example select the intended page link based on link text instead of link position (//div[@class='paginate']/a[text()='5']), but that alone doesn't help you much in this case.

The problem is of course that the site has a secondary pagination. You need to go to the next pagination page, before you can click of the next pagination links.

casper.wait(3000, function() {
    var nextButton = x('//*[@id="content"]/div[3]/a[text()="'+i+'"]');
    var lastPageNextButton = '.paginate > strong + a.next';
    var button = nextButton;

    if (this.exists(lastPageNextButton)) {
        button = lastPageNextButton;
    } else 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);
    });
});
Artjom B.
  • 61,146
  • 24
  • 125
  • 222