Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 让木偶演员等待给定文本出现/呈现在页面上?_Node.js_Puppeteer - Fatal编程技术网

Node.js 让木偶演员等待给定文本出现/呈现在页面上?

Node.js 让木偶演员等待给定文本出现/呈现在页面上?,node.js,puppeteer,Node.js,Puppeteer,我想加载一个页面,然后等待文本(在本例中是类)呈现,然后再获取内容 这个例子很有效 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 rende

我想加载一个页面,然后等待文本(在本例中是类)呈现,然后再获取内容

这个例子很有效

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();
}
但那只是永远挂在那里,什么都不会发生。。。 (老实说,我不明白querySelector是什么,所以问题可能就在那里)

我也试过:

    await page.waitForFunction('document.querySelector("scrapebot_description")');
    var checkText = "scrapebot_description"
    await page.evaluate((checkText) => {
        console.log("scrapebot_description FOUND IT!!");
    },{checkText});
这也不起作用

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

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

您可以执行以下操作:

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();
}
您可以这样做:

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();
}