我试图在Puppeteer中传递一个变量到page.evaluate()函数,但当我使用以下非常简化的示例时,变量evalVar是未定义的。
我找不到任何例子来构建,所以我需要帮助将该变量传递到page.evaluate()函数,这样我就可以在里面使用它。
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
const evalVar = 'WHUT??';
try {
await page.goto('https://www.google.com.au');
await page.waitForSelector('#fbar');
const links = await page.evaluate((evalVar) => {
console.log('evalVar:', evalVar); // appears undefined
const urls = [];
hrefs = document.querySelectorAll('#fbar #fsl a');
hrefs.forEach(function(el) {
urls.push(el.href);
});
return urls;
})
console.log('links:', links);
} catch (err) {
console.log('ERR:', err.message);
} finally {
// browser.close();
}
})();
你必须像这样将变量作为参数传递给pageFunction:
const links = await page.evaluate((evalVar) => {
console.log(evalVar); // 2. should be defined now
…
}, evalVar); // 1. pass variable as an argument
你可以通过向page.evaluate()传递更多参数来传入多个变量:
await page.evaluate((a, b c) => { console.log(a, b, c) }, a, b, c)
参数必须可以序列化为JSON或浏览器内对象的JSHandles: https://pptr.dev/#?show=api-pageevaluatepagefunction-args
单变量:
你可以使用下面的语法将一个变量传递给page.evaluate():
await page.evaluate(example => { /* ... */ }, example);
注意:您不需要将变量括在()中,除非您要传递多个变量。
多个变量:
你可以使用以下语法将多个变量传递给page.evaluate():
await page.evaluate((example_1, example_2) => { /* ... */ }, example_1, example_2);
注意:将变量包含在{}内是不必要的。
对于传递函数,有两种方法。
// 1. Defined in evaluationContext
await page.evaluate(() => {
window.yourFunc = function() {...};
});
const links = await page.evaluate(() => {
const func = window.yourFunc;
func();
});
// 2. Transform function to serializable(string). (Function can not be serialized)
const yourFunc = function() {...};
const obj = {
func: yourFunc.toString()
};
const otherObj = {
foo: 'bar'
};
const links = await page.evaluate((obj, aObj) => {
const funStr = obj.func;
const func = new Function(`return ${funStr}.apply(null, arguments)`)
func();
const foo = aObj.foo; // bar, for object
window.foo = foo;
debugger;
}, obj, otherObj);
您可以在test的启动选项中添加devtools: true
与上面@wolf的答案略有不同。使代码在不同上下文之间更容易重用。
// util functions
export const pipe = (...fns) => initialVal => fns.reduce((acc, fn) => fn(acc), initialVal)
export const pluck = key => obj => obj[key] || null
export const map = fn => item => fn(item)
// these variables will be cast to string, look below at fn.toString()
const updatedAt = await page.evaluate(
([selector, util]) => {
let { pipe, map, pluck } = util
pipe = new Function(`return ${pipe}`)()
map = new Function(`return ${map}`)()
pluck = new Function(`return ${pluck}`)()
return pipe(
s => document.querySelector(s),
pluck('textContent'),
map(text => text.trim()),
map(date => Date.parse(date)),
map(timeStamp => Promise.resolve(timeStamp))
)(selector)
},
[
'#table-announcements tbody td:nth-child(2) .d-none',
{ pipe: pipe.toString(), map: map.toString(), pluck: pluck.toString() },
]
)
也不是功能内部管道不能使用这样的东西
// incorrect, which is i don't know why
pipe(document.querySelector)
// should be
pipe(s => document.querySelector(s))