bp123
bp123

Reputation: 3417

setRequestInterception(false) returns an error

I'm using puppeteer to scrape a number of sites. The requirements for each site are different. I trying to use page.setRequestInterception(true); for some sites and then turn off for other sites. The example below starts page.setRequestInterception(true); but causes the error Error: Request Interception is not enabled! when I run await page.setRequestInterception(false);. What am I doing wrong?

Path: Controller

async function controller(page, logger) {
  await page.setRequestInterception(true);
  page.on('request', (req) => {
    if (
      req.resourceType() == 'stylesheet' ||
      req.resourceType() == 'font' ||
      req.resourceType() == 'image'
    ) {
      req.abort();
    } else {
      req.continue();
    }
  });

  await Promise.all([
    page.waitForNavigation(),
    page.goto('https://site-one.com')
  ]);

  await page.setRequestInterception(false);

  await Promise.all([
    page.waitForNavigation(),
    page.goto('https://site-two.com')
  ]);
}

Upvotes: 0

Views: 3674

Answers (1)

vsemozhebuty
vsemozhebuty

Reputation: 13792

I think the error is produced not by the await page.setRequestInterception(false); call but by 'request' event handler after interseption is toggled off. You need to cansel the event listener as well. See the last example in the page class intro.

Upvotes: 1

Related Questions