2015-11-19 3 views
1

Я застрял со сценарием CasperJS:PhantomJS и CasperJS нажав на ссылку и получить HTML

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

Я хочу, чтобы открыть ссылку в коде, чем нажать предыдущий и получить HTML-страницы (Я хочу получить html-документ за весь сезон с каждым результатом матча).

Что я делаю неправильно? (Я стартер)

Спасибо ..

+0

Вы пытаетесь использовать DOM вне контекста страницы, что невозможно. Во всяком случае, почему вы не используете функцию клика CasperJS? 'casper.click (selector)' –

ответ

2

Сценарий почти сразу. Единственная ошибка при взаимодействии со страницей (нажатие кнопки «предыдущая»).

Вы не можете получить доступ к элементам страницы изнутри скрипта, вам необходимо оценить («ввести») этот код внутри контекста открытой веб-страницы. В 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 Б. предложил.

+0

Спасибо, 100% РАБОТА! ;) – ohdecas

Смежные вопросы