简体   繁体   English

Puppeteer 的 `page.evaluate` 结果与 devTools 控制台不同

[英]Puppeteer's `page.evaluate` result differs from devTools console

I need to check for a certain service worker being registered.我需要检查某个正在注册的服务人员。 Unfortunately, page.evaluate returns undefined no matter what I do.不幸的是,无论我做什么, page.evaluate返回undefined

let page = await chrome.newPage();
await page.goto('http://127.0.0.1:8089/');
await page.waitFor(10000);

const isCorrectSW = await page.evaluate(async () => {
    await navigator
        .serviceWorker
        .getRegistrations()
        .then(registrations =>
            registrations[0].active.scriptURL.endsWith('/target.js')
        );
});
console.log(isCorrectSW);

isCorrectSW ends up being undefined , but if I enable devtools and run the same statement in the Chromium instance's devtools, I get the correct result. isCorrectSW最终是undefined ,但是如果我启用 devtools 并在 Chromium 实例的 devtools 中运行相同的语句,我会得到正确的结果。 I can also observe the service worker attached in the browser's dev tools.我还可以观察浏览器开发工具中附加的服务工作者。

Is this a Puppeteer bug, or am I doing something incorrectly?这是 Puppeteer 的错误,还是我做错了什么?

According to the documentation, page.evaluate returns undefined when the function passed returns a non-serializable value.根据文档,当传递的函数返回不可序列化的值时, page.evaluate返回 undefined。

In your scenario, the function you are passing into page.evaulate does not return anything.在您的场景中,您传递给 page.evaulate 的函数不会返回任何内容。

You are already using async, you can switch the function you are passing to be:您已经在使用异步,您可以将传递的函数切换为:

async () => {
    const registrations = await navigator.serviceWorker.getRegistrations()
    return registrations[0].active.scriptURL.endsWith('/target.js')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM