CasperJS/PhantomJS Segmentation fault

2019-06-09 07:37发布

问题:

I have a script which opens the urls in the links array and for each url it extracts the links in that url and inserts the new links to the array links(addLinks function). The code results in segmentation fault and it happens when it calls this.start to open a valid url (3rd line of the code). Is it a problem with casperjs or my code?

Another interesting point is that it always prints 'OK, it is loaded' after the 'Page title: ' while according to the code they should be printed in reverse order. Would you please tell me the reason for this strange behaviour?

// Just opens the page and prints the title
function start(link) {
    this.echo('lets try the link:--  '+ link + ' -------------');
    this.start(link, function() {
        this.echo('Page title: ' + this.getTitle());
    });
    this.echo('OK, it is loaded\n');
}

function check() {
    if (links[currentLink] && currentLink < upTo) {
        this.echo('--- Link ' + currentLink + ' ---');
        start.call(this, links[currentLink]);
        addLinks.call(this, links[currentLink]);
        currentLink++;
        this.run(check);
    } else {
        this.echo("All done.");
        this.exit();
    }
}
casper.start().then(function() {
    this.echo("Starting");
});

casper.run(check);

Here is the result of my code:

--- Link 0 ---
lets try the link:--  http://yahoo.com -------------
OK, it is loaded

Page title: Yahoo
111 links found http://yahoo.com
13 scripts found http://yahoo.com
0 frames found http://yahoo.com
 frame src:
new frame src:
--- Link 1 ---
lets try the link:--  http://everything.yahoo.com/ -------------
OK, it is loaded

PhantomJS has crashed. Please read the crash reporting guide...
Segmentation fault (core dumped)

回答1:

Use casper.start and casper.run only once in your script. You can rename this.start to this.thenOpen and this.run to this.then.