问题
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