I'm using nightmareJS and was trying to webscrape by using document.getElementsByClassName, this is the code:
import Nightmare from 'nightmare'
const nightmare = Nightmare({ show: true })
var name = name
nightmare
.goto('https://www.amazon.com/')
.insert("input[aria-label='Search']", 'impressora')
.click("input[value='Go']")
.wait(2000)
.evaluate(function(){
let text = document.getElementsByClassName('a-offscreen')[0];
name = text;
return name;
}).then(function (name) {
console.log('Price:', name)
});
The output is this:
Price: [object HTMLSpanElement]
What I would like to get is the price which is in the class "a-offscreen". Can anyone help? Thank you.
document.getElementsByClassName(class);
returns a collection of elements having the queried class but not their content so you have to access innerText
property of your element:
let text = document.getElementsByClassName('a-offscreen')[0].innerText;
name = text;
return name;
The output would be:
Price: $199.99