I am using nightmare.js for web scripting on a Chinese e-commerce website taobao(www.taobao.com/). The goal is to get product information. The code is very similar to the yahoo example code but the result is always null. I tried to put console.log to debug and realized the mistake might lay in the querySelector. Below is the code if someone has the time to take a look. Really appreciate it.
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
nightmare
.goto('https://www.taobao.com')
.type('form[action*="/search"] [name=q]', 'hellow kitty')
.click('form[action*="/search"] [type=submit]')
.wait(2000)
.evaluate(function () {
return document.querySelector('.row.row-2.title a')
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});
First and foremost: double-check your selectors and make sure they exist.
Setting that aside, the issue lay with return document.querySelector('.row.row-2.title a')
. DOM lists (like those returned from document.querySelector
) do not serialize nicely. Try pulling the results you want out before returning - say you wanted the HREFs from the anchors, you could do something like (from the hip):
return Array.from(document.querySelector('.row.row-2.title a')).map(a=> a.href);