PhantomJS 和 CasperJS 点击 link 得到 html

PhantomJS and CasperJS clicking a link and get html

我受困于 CasperJS 脚本:

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();

我想在代码中打开 link,然后单击上一步并获取页面的 html(我想获取完整季节的 html 文档每场比赛结果)。

我做错了什么? (我是首发)

谢谢..

剧本差不多吧。唯一的错误是在与页面交互时(单击 "previous" 按钮)。

您无法从脚本内部访问页面元素,您必须在打开的网页上下文中评估 ("inject") 该代码。在 CasperJS 中,有 casper.evaluate() 函数可以做到这一点。

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();

或者,您可以简单地写

而不是使用 casper.evaluate
this.click('#page_competition_1_block_competition_matches_6_previous');

正如 Artjom B. 建议的那样。