ohdecas
ohdecas

Reputation: 29

PhantomJS and CasperJS clicking a link and get html

I am stuck with a CasperJS script:

var casper = require('casper').create();
var fs=require('fs');

casper.start('http://int.soccerway.com/national/switzerland/super-league/20152016/regular-season/r31601/matches/?ICID=PL_3N_02', function() {
  this.wait(2000, function() {
    fs.write("swiss.html", this.getHTML() );

  });
  this.wait(2000, function() {
    var evObj = document.createEvent('Events');
    evObj.initEvent('click', true, false);
    document.getElementById('page_competition_1_block_competition_matches_6_previous').dispatchEvent(evObj);
  });
  this.wait(2000, function() {
    fs.write("swiss2.html", this.getHTML() );   
  });
});

casper.run();

I want to open the link in the code, than click previous and get an html of the page (I want to get an html document of the complete season with every match result).

What i am doing wrong? (I am a starter)

Thank you..

Upvotes: 1

Views: 501

Answers (1)

Vaviloff
Vaviloff

Reputation: 16838

The script is almost right. The only mistake is when interacting with page (clicking "previous" button).

You cannot access page elements from inside of the script, you have to evaluate ("inject") that code inside the opened web page context. In CasperJS, there's casper.evaluate() function to do that.

var casper = require('casper').create();
var fs=require('fs');

casper.start('http://int.soccerway.com/national/switzerland/super-league/20152016/regular-season/r31601/matches/?ICID=PL_3N_02', function() {
  this.wait(2000, function() {
    fs.write("swiss.html", this.getHTML() );

  });
  this.wait(2000, function() {

        // Code inside of this function will run 
        // as if it was placed inside the target page.
        casper.evaluate(function(term) {

            var evObj = document.createEvent('Events');
            evObj.initEvent('click', true, false);
            var prev_link = document.getElementById('page_competition_1_block_competition_matches_6_previous');
            prev_link.dispatchEvent(evObj);

        });

  });


  this.wait(2000, function() {
    fs.write("swiss2.html", this.getHTML() );   
  });
});

casper.run();

Or, instead of using casper.evaluate, you could simply write

this.click('#page_competition_1_block_competition_matches_6_previous');

as Artjom B. suggested.

Upvotes: 2

Related Questions