Federico Li
Federico Li

Reputation: 13

puppeteer cannot control 3rd tab

I'm trying to have my puppeteer script open 4 tabs each going to a different url. What I have encountered is that I can have the script successfully control the first 2 tabs opened in my chrome driver. However, whenever it gets to the 3rd tab, I get an error saying

"(node:8141) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'goto' of undefined (node:8141) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code."

let pages = await browser.pages();
const p1 = 'https://www.page.com/tab1';
const p2 = 'https://www.page.com/tab2';
const p3 = 'https://www.page.com/tab3';
const p4 = 'https://www.page.com/tab4';
const ptest = 'https://www.test.com';


    await browser.newPage();
    await browser.newPage();
    await browser.newPage();
    await browser.newPage();

    await pages[0].goto(p1, {waitUntil: 'load'});
    await pages[1].goto(p1, {waitUntil: 'load'});
    await pages[2].goto(p1, {waitUntil: 'load'});
    await pages[3].goto(p1, {waitUntil: 'load'});

Upvotes: 1

Views: 363

Answers (2)

Thomas Dondorf
Thomas Dondorf

Reputation: 25260

Problem

The error is thrown because browser.pages() resolves to an array of all currently open pages. If we look at it line by line, what happens is the following (assuming you start with a "fresh" browser):

let pages = await browser.pages();

pages now is an array with one page (the default blank page after the browser is started).

await browser.newPage();
await browser.newPage();
await browser.newPage();
await browser.newPage();

You create four new pages, but this does not automatically update your pages array. pages still contains only one page.

await pages[0].goto(p1, {waitUntil: 'load'});

The first call works as pages[0] contains a page.

await pages[1].goto(p1, {waitUntil: 'load'});

This will throw an error (Cannot read property 'goto' of undefined) as pages[1] is undefined.

Fix (and code improvement)

Move your line let pages = await browser.pages(); down in your code to be called before you use the pages. To improve your code you can also remove one browser.newPage() call, as one page is already present (right now you actually have five open pages) and to speed up your code you can use Promise.all:

const p1 = 'https://www.page.com/tab1';
const p2 = 'https://www.page.com/tab2';
const p3 = 'https://www.page.com/tab3';
const p4 = 'https://www.page.com/tab4';
const ptest = 'https://www.test.com';

await Promise.all([ // opens all pages at once
    browser.newPage(),
    browser.newPage(),
    browser.newPage(),
]);

let pages = await browser.pages(); // now resolves with four pages

await pages[0].goto(p1, {waitUntil: 'load'});
await pages[1].goto(p1, {waitUntil: 'load'});
await pages[2].goto(p1, {waitUntil: 'load'});
await pages[3].goto(p1, {waitUntil: 'load'});

Upvotes: 2

Nikhil Kadam
Nikhil Kadam

Reputation: 143

these issue is might be because of http errors or browse version. please check below link those may help you

If it's http error try,

const browser = await puppeteer.launch({ignoreHTTPSErrors: true});

or if it's related to browser check this post https://github.com/GoogleChrome/puppeteer/issues/1182

Upvotes: 0

Related Questions