Puppeteer returns undefined for page.evaluate despite being resolved on devtools

泪湿孤枕 提交于 2021-01-05 09:04:29

问题


I have a simple code in which I expect to find the element that contains certain text as such

  await page.goto('https://www.reddit.com/r/koreanvariety/comments/hsdt4j/the_great_escape_season_3_e12_back_to_the/')
  
  await page.waitFor(2000);

  const findComment = await page.evaluate(() => {
    return Array.from(document.querySelectorAll('a')).find(el => el.textContent === 'sometext' )
  })

  console.log('findComment', findComment)

And although the code above works on devtools it returns undefined in my windows console.

I believe the page is not fully loaded by the time this request is made however I have not been able to get any results back when resorting to await page.waitFor(2000);.

How do I get data back from page.evaluate?


回答1:


This is expected behavior

In order to get data from page.evaluate one must return serializable value, i.e. a value that can be stringified with JSON.stringify.

From the documentation:

If the function passed to the page.evaluate returns a non-Serializable value, then page.evaluate resolves to undefined.

Solution

A whole DOM node (that you find in your script) cannot be serialized because it has circular references. You will have to choose which data from the node you want and explicitly return those:

  const findComment = await page.evaluate(() => {
    return Array.from(document.querySelectorAll('a')).find(el => el.textContent === 'sometext' )?.href
  })


来源:https://stackoverflow.com/questions/62944390/puppeteer-returns-undefined-for-page-evaluate-despite-being-resolved-on-devtools

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!