I'm writing a small script to query some ads on an e-commerce website. My code is like this
var keywords = ['fender', 'yamaha'];
var casper = require('casper').create(),
url = 'http://www.somecommercesite.com';
casper.start(url, function () {
casper.each(keywords, function (self, word) {
casper.echo("Getting entries for keyword " + word + "..");
casper.fill('form#frmsearch', {
'q': word
}, true);
casper.then(function () {
casper.echo("KEYWORD SHOULD BE: " + word);
var actualKeyword = casper.evaluate(function() {
return "ACTUAL KEYWORD: " + document.getElementsByTagName('strong')[0].innerText;
});
casper.echo(actualKeyword);
var result = casper.evaluate(function () {
var entry = document.getElementsByClassName("listing")[0];
return entry.innerText;
});
casper.echo(result);
});
});
});
casper.run();
However, I got incorrect results. The output was
Getting entries for keyword fender..
Getting entries for keyword yamaha..
KEYWORD SHOULD BE: fender
ACTUAL KEYWORD: fender
// entries for fender
KEYWORD SHOULD BE: yamaha
ACTUAL KEYWORD: fender
// entries for fender
So, I got fender entries twice and never got yamahas. What's wrong with those code?