1

im trying to scrape a price element from a website using the page.evaluate method, but it allways returns {} when i console.log the result. here's the code:

const browser = await pptr.launch()
const page = await browser.newPage()
await page.goto("url")
const price = await page.evaluate(() => {
    return document.querySelector("selector")
})
console.log(price) // returns {}

I've tried using page.$(selector) too, it returns an array containing ElementHandle and bunch of other stuuf whitch i don't fully understand what they are

thanks!

  • Are you sure the elector is valid and returns someing? – ShobiDobi Mar 15 '22 at 10:57
  • yes, when the selector is invalid it returns an error but it's not the case – Yuval Megidish Mar 15 '22 at 11:27
  • Please search the site before asking: [Puppeteer page.evaluate querySelectorAll return empty objects](https://stackoverflow.com/questions/46377955/puppeteer-page-evaluate-queryselectorall-return-empty-objects), [Puppeteer returning empty object](https://stackoverflow.com/questions/55017057/puppeteer-returning-empty-object), [Puppeteer page.evaluate returns empty object](https://stackoverflow.com/questions/66386478/puppeteer-page-evaluate-returns-empty-object). Thanks! – ggorlen Mar 15 '22 at 14:57

1 Answers1

2

You are trying to pass an element handle from the browser to your puppeteer application. To transfer data from the browser puppeteer uses JSON.stringify and the element handle gets transformed into an empty object.

You could try to pass something else back to your application like the text content of the element.

const browser = await pptr.launch()
const page = await browser.newPage()
await page.goto("url")
const price = await page.evaluate(() => {
    return document.querySelector("selector").textContent
})
console.log(price)
jkoch
  • 766
  • 1
  • 5