javascriptpaginationhyperlinkcasperjs

Casperjs: How to manipulate javascript paging( links overloaded by javascript)


Using casperjs, I'm working on scraping some informaion from a web site, in this page there is a paging created by overloading a link

<a id="ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2"     
href="javascript:;//ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2"><img 
src="themes/images/fleche-suivant.gif" alt="Aller à la page suivante"></a>

there are two methods to access to the next page:

  1. to click on the next button as shown in this image

  2. or to change the value of the field as shown in this pic and click the enter key on the keyboard

I've tried both methods but didn't work, could someone please help me to code one of them.

var mouse = require("mouse").create(casper);
var url = '';
var url2 = '';
var tst;
casper.test.begin('Scraping start', function(test) {
casper.start(url, function() {
    this.test.pass('Opened 1st page');
})
.thenOpen(url2, function(){
    this.test.pass('Opened 2nd page')
})
.then(function(){

 //these are the tow methodes I try to click on the next button
//this.mouse.click("#ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2");   
this.thenClick(' div.liens a#ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2');
})

.then(function() {
    tst = this.evaluate(function() {
    return 
__utils__.getFieldValue( 
'[name="ctl0$CONTENU_PAGE$resultSearch$numPageTop"]');   
});
})
.run(function() {
    console.log(tst);
    test.done();
});
});

the version of casperjs is 1.1.4 and I'm using the phantom browser


Solution

  • This normally navigates to 2nd page on my end. Try it, also you can save screenshot for more debugging purposes.

    var casper = require('casper').create();
    var url = 'https://www.marchespublics.gov.ma/index.php5?page=entreprise.EntrepriseAdvancedSearch&AllCons&EnCours&domaineActivite=1.15';
    
    casper
        .start(url, function() {
            this.echo('Opened page ' + this.evaluate(function() {
                return document.title;
            }), 'INFO');
        })
        .then(function() {
            if (this.exists('a[id="ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2"]')) {
                this.echo('the heading exists');
            } else {
                this.echo('Does not exist');
            }
        })
        .thenClick('div.liens a#ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2')
        .wait(5000)
        .then(function() {
            this.capture('exit.png');
        })
        .run();
    

    Hope that helps!