casperjs click() a form element to submit, wait, run queries on next page?

Viewed 19181

I would like to click a submit button, wait for the next page to load, then obtain html on that second page.. I do the start, then and run, but the then step is still run on the first page. Any ideas?

var casper = require('casper').create();
var site = 'http://www.example.com';
var data = {}; 

casper.start(site, function() {
     this.evaluate(function() {
        $('input[type="submit"]:first').click();
    }); 
});

casper.then(function() {
    data.body = this.evaluate(function() {
        var rows = $('#content table:first tbody tr');
        var listings = rows.eq(3).text();
        var count = rows.eq(4).text();
        return {
            listings: listings,
            count: count
        };  
    }); 
});

casper.run(function() {
    this.echo(data.body.listings);
    this.exit();            
});
3 Answers
Related