问题描述
我正在使用 playwright 从页面上抓取一些数据。我需要调用一个页面函数来改变浏览器的状态来收集额外的信息。
我不断收到错误消息:UnhandledPromiseRejectionWarning: page.evaluate: Evaluation Failed: TypeError: cb is not a function
这是我目前所拥有的:
const { chromium} = require("playwright");
(async()=>{
this.browser = await chromium.launch({
headless: true,});
this.context = await this.browser.newContext();
this.page = await this.context.newPage();
this.page.goto('http://fakeExample.org')
const callbackHandle = await this.page.$('[data-callback]');
const cbName = await callbackHandle.evaluate(element=>element.getAttribute('data-callback')); //returns actual page function name 'myPageFunction'
this.page.evaluate((cb) => {
cb() //should call myPageFunction() on the page
},cbName)
})()
解决方法
我认为它归结为 window[cb]()
或 eval(cb)
,因为您传入的是带有函数名称的字符串。
关于这个主题的一些阅读: