【问题标题】:node.js puppeteer evaluate() returning Unexpected objects. throws TypeError each timenode.js puppeteer evaluate() 返回 Unexpected 对象。每次抛出 TypeError
【发布时间】:2021-09-25 18:28:45
【问题描述】:

我不明白为什么下面的代码抱怨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.

【问题讨论】:

    标签: javascript node.js async-await puppeteer headless


    【解决方案1】:

    不幸的是,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 {}
    

    【讨论】:

      【解决方案2】:

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

      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.
      

      【讨论】:

      • 我试过这个const check=await page.evaluate(() =&gt; { return Promise.resolve(function () { try { return document.getElementsByClassName("header-buttons")[0].childNodes[0].textContent.trim() !== "Login"; } catch (e) { console.log(e); return false; } }); });
      猜你喜欢
      • 1970-01-01
      • 2018-03-04
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      相关资源
      最近更新 更多