最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer: use function inside page.evaluate - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

page.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.
发布评论

评论列表(0)

  1. 暂无评论