【发布时间】:2021-07-25 09:53:59
【问题描述】:
每当我尝试传递一个函数时,像这样:
var myFunc = function() { console.log("lol"); };
await page.evaluate(func => {
func();
return true;
}, myFunc);
我明白了:
(node:13108) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: TypeError: func is not a function
at func (<anonymous>:9:9)
(node:13108) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
为什么?如何正确操作?
谢谢!
€:让我澄清一下:我这样做是因为我想先找到一些 DOM 元素并在该函数中使用它们,更像这样(简化):
var myFunc = function(element) { element.innerHTML = "baz" };
await page.evaluate(func => {
var foo = document.querySelector('.bar');
func(foo);
return true;
}, myFunc);
【问题讨论】:
标签: javascript node.js google-chrome puppeteer