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')
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();
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With