Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
Closing Session Puppeteer Node Js

i need somebody's help because i don't understand what's going on. So basically i built a script that opens a website and extracts text from the td's on one table the website has. After he extracts he will click on the next button and re-extract the text again since it changed. When he successfully extracts all of them, he must close the browser and he does, but idk why after closing it throws this

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

Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed. at CDPSession.send (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:214:35) at ExecutionContext._evaluateInternal (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:204:50) at ExecutionContext.evaluateHandle (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:155:21) at WaitTask.rerun (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:540:37)

My code

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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Session is getting closed because you have these two lines inside your if statement:

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

So, you can instead move them under the for loop as below and error will disappear. And also delete this line: j = 1; I didn't understand the use of this line and apparently it is also a part of the error.

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

When you do like this, the code will scrape 2 and half pages (instead of 1 page). Please let me know if it works.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda