Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!