无法在远程服务器上找到元素匹配选择器

Failed to find element matching selector on remote server

我正在为我们的区块链验证器制作一个监控工具。

此工具将从 url(示例)中抓取 HTML,并检查绿色跨度 Active 是否存在。我正在使用 puppeteer 进行抓取,但我遇到了问题。

当我在主机上运行下面的代码时,它能够获取元素。但是,当我 运行 它在远程服务器上时,它显示错误:

    err: {
      "type": "Error",
      "message": "Error: failed to find element matching selector \".cmuk-badge\"",
      "stack":
          Error: Error: failed to find element matching selector ".cmuk-badge"
              at ElementHandle.$eval (/home/ec2-user/validator-crypto-checker/node_modules/puppeteer/src/common/JSHandle.ts:1032:13)
              at processTicksAndRejections (internal/process/task_queues.js:97:5)
    }

服务器

Amazon Linux 2
Node v12.22.12
Dependencies installed following: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix

代码:

  const browser = await Puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto(url);

  try {
    const activeElement = ".cmuk-badge";
    const activeStatus = await page.$eval(
      activeElement,
      (node) => (<HTMLElement>node).innerText
    );
    logger.info(activeStatus)
    if (activeStatus !== "Active") throw new Error("Died");
    logger.info("Validator ok");
  } catch (err) {
    logger.error(err);
  }

  await browser.close();

我没有使用 JSX,但这对我来说很好

import Puppeteer from 'puppeteer';

const url = "https://explorer.persistence.one/validators/persistencevaloper1hndk2s0dx9p0pxd9pxwmls3eywpdu5ha76kpqs";

const browser = await Puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(url);
const badge = await page.waitForSelector('.cmuk-badge');
const activeStatus = await badge.evaluate(node => node.innerText);
console.log(activeStatus)
await browser.close();

当我 运行 以下内容时,没有任何记录。

await page.$eval(
    '.cmuk-badge',
    node => console.log(node)
);

所以也许您可以只使用 waitForSelectorevaluate 而不是 $eval。