Is there a way to add script to add new functions in evaluate() context of chrome+puppeeter?

后端 未结 2 365
Happy的楠姐
Happy的楠姐 2021-01-14 21:44

Based on this response, is there a way (like with casperjs/phantomjs) to add our custom functions in page.evaluate() context ?

By example, include a file with a help

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 22:11

    You can register helper functions in separate page.evaluate() function. page.exposeFunction() looks temptingly, but it don't have access to browser context (and you need document object).

    Here is an example of registering helper function with $x():

    const puppeteer = require('puppeteer');
    
    const helperFunctions = () => {
        window.$x = xPath => document
            .evaluate(
                xPath,
                document,
                null,
                XPathResult.FIRST_ORDERED_NODE_TYPE,
                null
            )
            .singleNodeValue;
    };
    
    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto('https://en.wikipedia.org', { waitUntil: 'networkidle2' });
    
        await page.evaluate(helperFunctions);
    
        const text = await page.evaluate(() => {
            // $x() is now available
            const featureArticle = $x('//*[@id="mp-tfa"]');
    
            return featureArticle.textContent;
        });
        console.log(text);
        await browser.close();
    })();
    

    (edit - add helpers from a file)

    You can also keep helpers in a separate file and inject it into browser context by page.addScriptTag(). Here is an example of it:

    helperFunctions.js

    window.$x = xPath => document
        .evaluate(
            xPath,
            document,
            null,
            XPathResult.FIRST_ORDERED_NODE_TYPE,
            null
        )
        .singleNodeValue;
    

    And use it:

    const puppeteer = require('puppeteer');
    
    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto('https://en.wikipedia.org', { waitUntil: 'networkidle2' });
    
        await page.addScriptTag({ path: './helperFunctions.js' });
    
        const text = await page.evaluate(() => {
            // $x() is now available
            const featureArticle = $x('//*[@id="mp-tfa"]');
    
            return featureArticle.textContent;
        });
        console.log(text);
        await browser.close();
    })();
    

提交回复
热议问题