CasperJS querySelectorAll (variable) in function returns no results

I am in the process of writing a site scraper to grab some specific content from an ajax site without actual links, only with text that can be clicked. I have been using javascript for about a week now and have been using CasperJS since it cuts out a lot of work.

The problem I find is that I am writing several functions that all do the same thing, just find different links depending on the page. Therefore, I have:

function getLinks() {
    var links = document.querySelectorAll('div.AjaxLink h3');
    return Array.prototype.map.call(links, function(link) {
        return link.innerText;
    });
}

Its launch is carried out through:

casper.then(function() {
    var myLinks = this.evaulate(getLinks);
    /* ... link manipulation code code ... */
});

It works great. I obviously don't want to have half a dozen functions that just have a different query string. So I want to do the following:

function getLinks(findText) {
    var links = document.querySelectorAll(findText);
    return Array.prototype.map.call(links, function(link) {
        return link.innerText;
    });
}

Then I try to run it through:

casper.then(function() {
    var myLinks = getLinks('div.AjaxLink h3');
    /* ... link manipulation code code ... */
});

findText , , NodeList.

? , ?

+4
1

CasperJS PhantomJS. PhantomJS . , evaluate() , require phantom. , window document, document , DOM . querySelectorAll() . DOM evaluate().

, casper.evaluate(). evaluate(), :

function getLinks(findText) {
    ...
}

casper.then(function() {
    var myLinks = this.evaluate(getLinks, 'div.AjaxLink h3'); // THIS
    ...
});

evaluate :

. evaluate . : JSON, .

, , DOM .. !

+5

All Articles