Puppeteer:在waitForNavigation 后page.evaluate 不起作用,调试在page.evaluate 内部不起作用 [英] Puppeteer: page.evaluate not working after waitForNavigation, debug not working inside page.evaluate for document

查看:181
本文介绍了Puppeteer:在waitForNavigation 后page.evaluate 不起作用,调试在page.evaluate 内部不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am able to navigate to a page using puppeteer but afterwards the page.evaluate is not returning any response. Further, I am unable to debug inside the page.evaluate either. I run the script in debug mode (node debug filename.js), use sb(15) to set the breakpoint on line 15, press c to continue, wait for the page to load, then enter 'repl'. Now when I try to debug it says document is not defined. How do I solve these two issues?

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() 等待.

Selecting a city does not cause the URL to change, which is what page.waitForNavigation() waits for.

这会在页面导航到新 URL 或重新加载时解决.

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

它永远不会发生,所以你的代码不会继续.

It never happens, so your code does not continue.

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

You might be looking for page.waitForSelector() instead:

  // ...
  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();

这篇关于Puppeteer:在waitForNavigation 后page.evaluate 不起作用,调试在page.evaluate 内部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆