简体   繁体   中英

cannot return document.querySelector with nightmare API on node.js

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM