让Puppeteer等待给定文本在页面上显示/呈现?

时间:2022-12-20 19:28:53

I want to load a page, and then wait for the text (or class in this case) to be rendered before I get the content.

我想加载一个页面,然后在获取内容之前等待文本(或本例中的类)的呈现。

This example works.

这个例子有效。

async function test() {

    const browser = await puppeteer.launch();
    const page    = await browser.newPage();
    await page.goto('https://sunnythailand.com');

    // Wait until the page is fully rendered
    while (content.indexOf("scrapebot_description") < 0) {
        console.log("looking for scrapebot_description")    
        await new Promise((resolve)=>setTimeout(()=> resolve() ,1000));
        content = await page.content();
    }
    console.log("scrapebot_description FOUND!!!")   

    await browser.close();
}

My question is, can I do this easier with puppeteer?

我的问题是,我可以用木偶操作者更轻松地做到这一点吗?

I tried this:

我试过这个:

    await page.waitForFunction('document.querySelector("scrapebot_description")');

But that just hangs there forever, nothing ever happens... (to be honest I dont understand what querySelector is, so perhaps the problem is there)

但那只是永远挂在那里,什么都没发生......(说实话我不明白querySelector是什么,所以也许问题就在那里)

I also tried this:

我也试过这个:

    var checkText = "scrapebot_description"
    await page.evaluate((checkText) => {
        console.log("scrapebot_description FOUND IT!!");
    },{checkText});

This also does not work.

这也行不通。

This is the last element to render on the page what im waiting for....

这是在页面上呈现的最后一个我正在等待的元素....

    <span class="hide scrapebot_description ng-binding" ng-bind="'transFrontDescription' | translate">

1 个解决方案

#1


2  

You can do this:

你可以这样做:

async function test() {
  const browser = await puppeteer.launch();
  const page    = await browser.newPage();
  await page.goto('https://sunnythailand.com');

  const selector = '.scrapebot_description' // or #scrapebot_description
  await page.waitForSelector(selector)

  console.log("scrapebot_description FOUND!!!")   

  await browser.close();
}

#1


2  

You can do this:

你可以这样做:

async function test() {
  const browser = await puppeteer.launch();
  const page    = await browser.newPage();
  await page.goto('https://sunnythailand.com');

  const selector = '.scrapebot_description' // or #scrapebot_description
  await page.waitForSelector(selector)

  console.log("scrapebot_description FOUND!!!")   

  await browser.close();
}