Casperjs click method

casperjs, javascript, web-scraping

Solution

This should be what you are looking for. Note that I moved `firstUrl` to be a global variable; this way, the `Casper.waitFor()` has access to it.

Also, using `this.evaluate()` inside of the `Casper.waitFor()` was unnecessary and actually inhibiting receiving the error message because neither `this` nor `firstUrl` existed on the remote page. This is because any variables that you want to have access to inside of an `Casper.evaluate()` must be passed as arguments after the function.

var casper = require('casper').create();
var x = require('casper').selectXPath;
var firstUrl;
casper.start('http://www.guru.com/emp/search.aspx?keyword=#&&page=1&sort=Earnings');

casper.then(function() {
    this.test.assertExists({
        type: 'xpath',
        path: '//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'
    }, "Got Here");
});

casper.then(function() {
    firstUrl = this.getCurrentUrl()
});

casper.thenClick(x('//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'), function() {
    console.log("Woop!");
});

casper.waitFor(function check() {
    return this.getCurrentUrl() != firstUrl;
}, function then() {
    console.log(this.getCurrentUrl());
});


casper.run();

This is the result that I get when running the code above:

Woop!
http://www.guru.com/emp/search.aspx?keyword=#&&sort=Earnings&page=2

Problem

I'm pretty new to casperjs and javascript in general, but I have pretty extensive coding experience in other realms. Currently the code I'm trying to get running is just going to a website and clicking on a link, which should be straightforward, but I'm having trouble. ``` var casper = require('casper').create(); var x = require('casper').selectXPath; casper.start('http://www.guru.com/emp/search.aspx?keyword=#&&page=1&sort=Earnings'); casper.then(function() { this.test.assertExists({ type: 'xpath', path: '//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]' }, "Got Here"); }); casper.then(function() { var firstUrl = this.getCurrentUrl() }); casper.thenClick(x('//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'), function() { console.log("Woop!"); }); casper.waitFor(function check() { return this.evaluate(function() { return this.getCurrentUrl() != firstUrl; }); }, function then() { console.log(this.getCurrentUrl()); }); casper.run(); ``` currently this times out after 5000ms without wrapping in the waitFor it simply prints the same url twice.

Original source