【问题标题】:Closing Session Puppeteer Node Js关闭会话 Puppeteer 节点 Js
【发布时间】:2023-03-25 08:53:01
【问题描述】:

我需要别人的帮助,因为我不明白发生了什么。所以基本上我构建了一个脚本,可以打开一个网站并从网站拥有的一张表上的 td 中提取文本。提取后,他将单击下一步按钮并再次重新提取文本,因为它已更改。当他成功提取所有这些时,他必须关闭浏览器并且他这样做了,但是我不知道为什么在关闭它之后会抛出这个

C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:214 return Promise.reject(新错误(Protocol error (${method}): Session closed. Most likely the ${this._targetType} has been closed.)); ^

错误:协议错误 (Runtime.callFunctionOn):会话已关闭。该页面很可能已关闭。 在 CDPSession.send (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:214:35) 在 ExecutionContext._evaluateInternal (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:204:50) 在 ExecutionContext.evaluateHandle (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:155:21) 在 WaitTask.rerun (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:540:37)

我的代码

const puppeteer = require('puppeteer');


var k = 0;
var i = 0;

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.pinksale.finance/#/launchpad/0xaD3cbb319c915104418c6fAd590EaB94C1940Ec1?chain=BSC');
  const element = await page.waitForSelector('li.ant-pagination-item:nth-child(8) > a:nth-child(1)');
  const value = await element.evaluate(el => el.textContent);
  const carteiras = await page.waitForSelector('table.has-text-centered > thead:nth-child(1) > tr:nth-child(1) > th:nth-child(2)');
  const numerodecarteiras = await carteiras.evaluate(el => el.textContent);
  var matches = numerodecarteiras.match(/(\d+)/)[0];
  for(i = 0; i<=value;i++)
  {
    for(let j = 1; j<=10;j++)
    {
      var a = 'table.has-text-centered > tbody:nth-child(2) > tr:nth-child(';
      var b = ') > td:nth-child(2) > div:nth-child(1)';
      const element = await page.waitForSelector(a+j+b);
      const valor2 = await element.evaluate(el => el.textContent);
      console.log(valor2);
      k++
      if(k == Number(matches))
      {
        console.log('Número de wallets extraídas --->',k);
        await page.close();
        await browser.close();
      }
    }
    j = 1;
    await page.click('.ant-pagination-next > button:nth-child(1)');
  }
})();

【问题讨论】:

  • await browser.close(); 之后,也许是return 以避免循环的进一步迭代在陈旧的浏览器/页面上运行?

标签: javascript node.js puppeteer puppeteer-sharp


【解决方案1】:

会话即将关闭,因为您的 if 语句中有这两行:

        await page.close();
        await browser.close();

因此,您可以将它们移到下面的 for 循环下,错误就会消失。并删除这一行:j = 1; 我不明白这一行的使用,显然这也是错误的一部分。

async () => {
  var k = 0;
  var i = 0;
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.pinksale.finance/#/launchpad/0xaD3cbb319c915104418c6fAd590EaB94C1940Ec1?chain=BSC');
  const element = await page.waitForSelector('li.ant-pagination-item:nth-child(8) > a:nth-child(1)');
  const value = await element.evaluate(el => el.textContent);
  const carteiras = await page.waitForSelector('table.has-text-centered > thead:nth-child(1) > tr:nth-child(1) > th:nth-child(2)');
  const numerodecarteiras = await carteiras.evaluate(el => el.textContent);
  var matches = numerodecarteiras.match(/(\d+)/)[0];
  for (var i = 0; i <= value; i++) {
    for (let j = 1; j<=10; j++) {
      var a = 'table.has-text-centered > tbody:nth-child(2) > tr:nth-child(';
      var b = ') > td:nth-child(2) > div:nth-child(1)';
      const element = await page.waitForSelector(a+j+b);
      const valor2 = await element.evaluate(el => el.textContent);
      console.log(valor2);
      k++
      if(k == Number(matches)) {
        console.log('Número de wallets extraídas --->',k);
      }
    }
    await page.click('.ant-pagination-next > button:nth-child(1)');
  }
  await res.end();
  await browser.close();
})

当您这样做时,代码将抓取 2 页半(而不是 1 页)。请让我知道它是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    相关资源
    最近更新 更多