node.js puppeteer evaluate() 返回意外对象。每次抛出 TypeError

node.js puppeteer evaluate() returning Unexpected objects. throws TypeError each time

我无法理解为什么以下代码抱怨 check() 不是函数。当我使它成为具有目标函数的对象时,它再次抱怨找不到这样的函数。我已经尝试解决这个问题大约 6 个小时了。任何帮助将不胜感激...

 const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    await page.goto('https://bet254.com');

 const check=await page.evaluate(() => {
            return function () {
            try {
                return document.getElementsByClassName("header-buttons")[0].childNodes[0].textContent.trim() !== "Login";
            } catch (e) {
                console.log(e);
                return false;
            }
        }
 });
if (check()) {
        console.log("Logged in already!");
    } else {}

错误如下:-

(node:9632) UnhandledPromiseRejectionWarning: TypeError: check is not a function
    at D:\void\js_\node_puppeteer\entry.js:19:9
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9632) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9632) [DEP0018] 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.

你应该从官方文档中寻找答案,而不是在这里提问。 这是有关此功能方法的文档。从最后一行你应该明白了

page.evaluate(pageFunction[, ...args])
pageFunction <[function]|[string]> Function to be evaluated in the page context
...args <...[Serializable]|[JSHandle]> Arguments to pass to pageFunction
returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of pageFunction
If the function passed to the page.evaluate returns a [Promise], then page.evaluate would wait for the promise to resolve and return its value.
If the function passed to the page.evaluate returns a non-[Serializable] value, then page.evaluate resolves to undefined. DevTools Protocol also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity, and bigint literals.

不幸的是,page.evaluate() 只能传输可序列化的值(大致上,值 JSON 可以处理)。函数不可序列化。因此,您可以尝试以下方法之一:

const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('https://bet254.com');

const check = await page.evaluate(() => {
    return document.getElementsByClassName("header-buttons")[0].childNodes[0].textContent.trim() !== "Login";
});

if (check) {
    console.log("Logged in already!");
} else {}
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('https://bet254.com');

function check(page) {
  return page.evaluate(() => {
      return document.getElementsByClassName("header-buttons")[0].childNodes[0].textContent.trim() !== "Login";
  });
}

if (await check(page)) {
    console.log("Logged in already!");
} else {}