2
I’m using the library Nightmare, for Web Scrapping, function works normally when I pass a string as parameter '.seletor', the problem occurs when I store this value in a variable and step as parameter, follows code below (I will use a product from Amazon as an example):
JS:
const nightmare = require('nightmare')()
async function checkPrice () {
const url = 'https://www.amazon.com.br/250GB-SAMSUNG-970-EVO-PCIe/dp/B07CGGNX7S/'
const priceNow = await nightmare.goto(url)
.wait('#priceblock_ourprice')
.evaluate(() => document.querySelector('#priceblock_ourprice').innerText)
.end()
console.log(priceNow) // R$664,90
}
checkPrice()
This way the product price is returned, without any kind of error...
Follow example with the error:
JS:
const nightmare = require('nightmare')()
async function checkPrice () {
const url = 'https://www.amazon.com.br/250GB-SAMSUNG-970-EVO-PCIe/dp/B07CGGNX7S/'
const priceSelector = '#priceblock_ourprice'
const priceNow = await nightmare.goto(url)
.wait(priceSelector)
.evaluate(() => document.querySelector(priceSelector).innerText)
.end()
console.log(priceNow) // Mensagem de erro
}
checkPrice()
Error message:

In which call the error (
priceSelectorapparently not set) is occurring? On the method call linewaitor in thequerySelector, who is in theevaluate? I suppose it’s in theevaluate, that probably makes a little bit of black Magic behind the scenes...– Luiz Felipe