Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 helper function x to call an Xpath function : x('//a/@href')

like image 284
MevatlaveKraspek Avatar asked Mar 08 '23 01:03

MevatlaveKraspek


1 Answers

You can register helper functions to run in the browser context in separate page.evaluate() calls. page.exposeFunction() looks tempting, but it doesn't have access to browser context (and you need the document object).

Here is an example of registering helper functions like $x() in the browser context:

const puppeteer = require('puppeteer');

const addHelperFunctions = () => {
    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(addHelperFunctions);

    const text = await page.evaluate(() => {
        // $x() is now available
        const featureArticle = $x('//*[@id="mp-tfa"]');

        return featureArticle.textContent;
    });
    console.log(text);
    await browser.close();
})();

You can also keep helpers in a separate file and inject them into the browser context using 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();
})();
like image 71
Everettss Avatar answered Mar 09 '23 14:03

Everettss