admin管理员组文章数量:1332973
I need to use a function inside page.evaluate
with puppeteer. I use exposeFunction
and I need to send the full Element to my function
I have a simple example:
const puppeteer = require('puppeteer');
const myFunction = (content) => {
console.log(content.outerHTML); // empty
}
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
page.on('console', msg => console.log('PAGE LOG:', msg.text()));
const url = "";
await page.goto(url,{
waitUntil: 'networkidle2'
})
await page.exposeFunction('myFunction', myFunction);
const content = await page.$('.content')
await page.evaluate( (content) => {
myFunction (content); // I need to send full Element
//console.log(content.outerHTML); // here works fine
//my_function (JSON.stringify(content)); // sends {}
}, content )
})()
I've tried to send with JSON.stringify / JSON.parse with no results.
I need to use a function inside page.evaluate
with puppeteer. I use exposeFunction
and I need to send the full Element to my function
I have a simple example:
const puppeteer = require('puppeteer');
const myFunction = (content) => {
console.log(content.outerHTML); // empty
}
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
page.on('console', msg => console.log('PAGE LOG:', msg.text()));
const url = "https://www.google.";
await page.goto(url,{
waitUntil: 'networkidle2'
})
await page.exposeFunction('myFunction', myFunction);
const content = await page.$('.content')
await page.evaluate( (content) => {
myFunction (content); // I need to send full Element
//console.log(content.outerHTML); // here works fine
//my_function (JSON.stringify(content)); // sends {}
}, content )
})()
I've tried to send with JSON.stringify / JSON.parse with no results.
Share Improve this question edited Jul 13, 2024 at 14:55 ggorlen 57.5k8 gold badges112 silver badges154 bronze badges asked Jun 5, 2019 at 10:08 kurtkokurtko 2,1264 gold badges32 silver badges49 bronze badges2 Answers
Reset to default 5page.exposeFunction
is limited to serialized data as the other answer already points out.
But you can define a function inside of the page.execute
block. Be aware, that functions defined there will only be present in the browser environment and not inside your Node.js script.
Code sample
The following code implements the myFunction
inside the evaluate
function and can then be used below:
await page.evaluate((content) => {
const myFunction = (content) => {
return content.outerHTML;
};
const result = myFunction(content);
return result; // or whatever you want to do with the result
}, content);
Short Answer: You cannot.
Long answer:
You cannot send DOM elements outside browser via exposeFunction
.
Moreover anything you send will be serialized, there cannot be any circular reference. NodeJS does not have a DOM.
So the solution is,
- either pass only the string and object without any circular reference,
- or use puppeteers ElementHandle to handle the elements. In your code, the
content
variable is a ElementHandle.
本文标签: javascriptPuppeteer use function inside pageevaluateStack Overflow
版权声明:本文标题:javascript - Puppeteer: use function inside page.evaluate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742345337a2457407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论