I'm using Nodejs and npm module Phantom to scrap a web page. The info that I need is placed with a ajax request when a span is clicked.
Objective: In site 'www.academiadasapostas.com/stats/team/961#tab=t_stats' I want to click in 'Bundesliga' button to scrap info.
Problem: I can't go directly to button url (www.academiadasapostas.com/stats/team/961#tab=t_stats&team_id=961&competition_id=9&page=1) and I don't know how to click button in Phantom.
My code:
var url = 'https://www.academiadasapostas.com/stats/team/961#tab=t_stats';
phantomInstance.createPage()
.then((page) => {
phantomPage = page;
return page.open(url);
})
.then((status) => {
phantomPage.evaluate(function() {
//trying click
return document.querySelectorAll('[data-id]')[1].click();
})
.then(function(){
return phantomPage.property('content');
})
.then((content) => {
// handle content of page
});
});
HTML snapshot:
<td>
<span class="competition all " data-id="0" onclick="teamAjax_Filterchange(this)" style="float: left; display: none;">Tudo
</span>
<span class="competition " data-id="9" onclick="teamAjax_Filterchange(this)">
<ul class="flag" title=""><li class="ar a80" title=""></li><li class="co c1"></li><li class="co chover"></li></ul>Bundesliga
</span>
<span class="competition " data-id="10" onclick="teamAjax_Filterchange(this)">
<ul class="flag" title=""><li class="ar a7" title=""></li><li class="co clc"></li><li class="co chover"></li></ul>UEFA Champions League
</span>
</td>
EDIT 1: I try this but seems doesn't work too:
phantomPage.evaluate(function() {
var ev = document.createEvent("MouseEvent");
ev.initMouseEvent(
"click",
true /* bubble */, true /* cancelable */,
window, null,
0, 0, 0, 0, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left*/, null
);
return document.querySelectorAll('[data-id]')[1].dispatchEvent(ev);
})