Puppeteer:page.evaluate 在 waitForNavigation 之后不工作,调试在文档 page.evaluate 内部不工作

Puppeteer: page.evaluate not working after waitForNavigation, debug not working inside page.evaluate for document

我可以使用 puppeteer 导航到一个页面,但之后 page.evaluate 没有返回任何响应。此外,我也无法在 page.evaluate 内部进行调试。我运行调试模式下的脚本(node debug filename.js),使用sb(15)在第15行设置断点,按c继续,等待页面加载,然后输入 'repl'。现在,当我尝试调试时,它显示 document is not defined。我该如何解决这两个问题?

const puppeteer = require('puppeteer');

(async function scrape() {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  page.setDefaultNavigationTimeout(90000);
  const url = "https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx";
  await page.goto(url, {waitUntil: 'networkidle2', timeout: 0});
  await page.waitForSelector('#ctl00_ContentPlaceHolder1_ddl_District');
  await page.select('#ctl00_ContentPlaceHolder1_ddl_District', '020');
  await page.waitForNavigation();
  let beds = await page.evaluate(() => {
    let dataRows = document.body.querySelectorAll("tbody tr");
    console.log("Num entires == " + dataRows.length);
  });
  await browser.close();
})();

选择城市不会导致 URL 发生变化,这是 page.waitForNavigation() 等待的结果。

This resolves when the page navigates to a new URL or reloads.

它永远不会发生,所以您的代码不会继续。

您可能正在寻找 page.waitForSelector()

  // ...
  await page.select('#ctl00_ContentPlaceHolder1_ddl_District', '020');
  await page.waitForSelector('tbody tr');
  let beds = await page.evaluate(() => {
    let dataRows = document.body.querySelectorAll('tbody tr');
    return [...dataRows].map(row => row.querySelector('h5').textContent);
  });
  console.log(beds);
  await browser.close();