0

So I got a basic puppeteer script working that does some basic stuff: Which looks like this -

import puppeteer from "puppeteer-extra";
import RecaptchaPlugin from "puppeteer-extra-plugin-recaptcha";
import { executablePath } from "puppeteer";
import { anonymizeProxy } from "proxy-chain";

async function main() {
  console.log("Starting ...");
  const proxy =
    "http://XXXXXXXXXXXXXXXXXXXXXX";
  const newProxyUrl = await anonymizeProxy(proxy);

  puppeteer.use(
    RecaptchaPlugin({
      provider: {
        id: "2captcha",
        token: "XXXXXXXXXXXXXXXXXX",
      },
    })
  );

  // Puppeteer usage as normal (headless is "false" just for this demo)
  const browser = await puppeteer.launch({
    headless: true,
    executablePath: executablePath(),
    args: [
      "--single-process",
      "--no-zygote",
      "--no-sandbox",
      `--proxy-server=${newProxyUrl}`,
    ],
  });

  try {
    const page = await browser.newPage();
    await page.setDefaultNavigationTimeout(180000);

    await page.goto("http://www.example.com");
    // do more things etc etc 
  
    console.log("Closing the page");
    await page.close();
    console.log("Closed the page");
    await browser.close();
    console.log("Closed the browser");
    await browser.process()?.kill();
    console.log("Force closed the browser");
  } catch (error) {
    console.log(error);
  } finally {
    await browser.close();
  }
}

main();

Now I would assume that if I started this as a node file then I would assume the node app to exit out after the console logs - "Force closed the browser" but that does not happen - it keeps running indefinitely in the terminal. Any idea how to fix this? I have already tried all the solutions in this stackoverflow and nothing seems to be helping - Puppeteer doesn't close browser

Indrajeet Haldar
  • 163
  • 1
  • 13
  • Not definitely a solution, but `browser.close()` belongs in a `finally` block so it executes even if your code throws. Beyond that, maybe try checking the browser flags. `await page.setDefaultNavigationTimeout(0);` is very dangerous, you can hang your script forever. Much safer is some reasonable timeout, say, 3 minutes. If something is hanging that long, the solution should be a clear exit and/or throw that you can catch and take action on, for example, resetting the page back to its original URL or whatever, not hanging forever. – ggorlen Dec 16 '22 at 17:34
  • Hello thanks for your response - I put in things in a try / catch / finally block. Thanks for the pointer with the navigation time out thing. Do let me know if you figured out a version of closing the browser. – Indrajeet Haldar Dec 16 '22 at 19:13
  • Well that should do it. If it's not closing, did you try removing a plugin? Maybe one of those is keeping it open? If you call `browser.close()` in a `finally` block it shouldn't be in two places, like in the `try` block, or you'll try to close it twice. – ggorlen Dec 16 '22 at 21:09
  • Didnt help - im going to try shelljs and a force quit I think – Indrajeet Haldar Dec 18 '22 at 20:24

0 Answers0