The code:
var array = casper.evaluate(function () {
var nodes = document.querySelectorAll('#J_bought_main > div > div:nth-child(4) > div:nth-child(3) > div > div');
return Array.prototype.map.call(nodes, function extractOrder(x) {
var textNode = x.querySelector('table:nth-child(2) > tbody > tr:nth-child(2) > td:nth-child(5) > div > p:nth-child(2)');
var urlNode = x.querySelector('table:nth-child(2) > tbody > tr:nth-child(2) > td:nth-child(6) > div > div:nth-child(2) > div:nth-child(1) > span > a');
return {
'text': textNode != null ? textNode.textContent : "",
'url': urlNode != null ? urlNode['href'] : ""
};
});
});
casper.echo(array.length);
And when I execute, it always fail with TypeError: null is not an object (evaluating 'array.length')
.
It seems the array which evaluate return is null
, but when I execute the same script in Chrome console, it gives the correct result:
(There are some Chinese characters but it doesn't matter)
So why the same code succeed in Chrome but failed in my CasperJS evaluate? And How write a correct one?
Any advice would be appreciated.
From the doc:
The concept behind this method is probably the most difficult to understand when discovering CasperJS. As a reminder, think of the evaluate() method as a gate between the CasperJS environment and the one of the page you have opened; everytime you pass a closure to evaluate(), you’re entering the page and execute code as if you were using the browser console.
From the doc it claims if it succeed in Chrome console and it will succeed in my evaluate
of CasperJS...