I want to scrape some web data using CasperJS. The data is in a table, in each row there is a link leading to a page with more detail. In the script there is a loop iterating through all table rows. I want Casper to click the link, collect the data on a sub-page and come one history step back to process next table row. The problem is that the click() doesn't work and I don't know why. Is there any way to fix this ? (note: a javascript function viewContact is invoked by href)
Here is the code :
var employee = {
last_name: "",
first_name: "",
position: "",
department: "",
location: "",
email: "",
phone: "",
twitter: ""
};
var employees = [];
var result_number = 50;
var start_url = 'https://www.jigsaw.com/SearchContact.xhtml?companyId=489781&orderby=0&order=0&opCode=paging&mode=0&estimatedCount=126&dead=false&rpage=1&rowsPerPage=200';
var casper = require('casper').create({
javascriptEnabled: true
});
casper.start(start_url, function() {
var js = this.evaluate(function() {
return document;
});
for (var i = 1; i <= result_number; i++)
{
// j stands for three neighbour td columns containing:
// position, name+link, location
employee.position = this.getHTML('#sortableTable tr:nth-child(' + i + ') td:nth-child(3) span');
// click link and get other data
this.click('#sortableTable tr:nth-child(' + i + ') td:nth-child(4) span a');
employee.first_name = this.getHTML('#sortableTable tr:nth-child(' + i + ') td:nth-child(4) span a');
//collect data
this.waitForSelector('#firstname', function() {
employee.first_name = this.getHTML('#firstname');
});
this.waitForSelector('#lastname', function() {
employee.last_name = this.getHTML('#lastname');
});
this.waitForSelector('#state', function() {
employee.department = this.getHTML('#state');
});
this.waitForSelector('#email', function() {
employee.email = this.getHTML('#email');
});
this.waitForSelector('#phone', function() {
employee.phone = this.getHTML('#phone');
});
//get back to previous page
this.back();
employee.location = this.getHTML('#sortableTable tr:nth-child(' + i + ') td:nth-child(5) span');
this.echo('\n\n Employee number: ' + i + " :\n");
this.echo('first name : ' + employee.first_name);
this.echo('last name : ' + employee.last_name);
this.echo('position : ' + employee.position);
this.echo('department : ' + employee.department);
this.echo('location : ' + employee.location);
this.echo('email : ' + employee.email);
this.echo('phone : ' + employee.phone);
}
});
casper.run();