Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

casperjs

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();            
});
like image 245
tester Avatar asked Aug 19 '12 04:08

tester


3 Answers

This only partially solves your problem, but you can confirm that you've made it to the second page using waitFor. For example:

this.waitFor(function check() {
    return (this.getCurrentUrl() === PAGE_2_URL);
},
function then() { // step to execute when check() is ok
    this.echo('Navigated to page 2', 'INFO');
},
function timeout() { // step to execute if check has failed
    this.echo('Failed to navigate to page 2', 'ERROR');
});
like image 179
Nate Avatar answered Nov 10 '22 20:11

Nate


Its a good idea use waitForResource to wait the page load finished, see documentation here documentation

example:

casper.waitForResource(function checkAuth(validcredentials) {
                    return validcredentials;
                }, function onReceived() {
                    if (authTitle !== this.getTitle()) {
                        this.log('Autenticação realizada com sucesso, aguarde...');
                    } else { 
                        // this.capture('pic3.png');
                        this.log('Usuario ou senha invalidos!', 'ERROR');
                        this.die('User or password invalids!', 1); }
                });
like image 31
Rodrigo Andrade Avatar answered Nov 10 '22 21:11

Rodrigo Andrade


I have a similar problem with doubleclick. Make sure the click event is actually fired. I suspect this is the cause of running the next step within the same content.

like image 1
user337620 Avatar answered Nov 10 '22 22:11

user337620