Puppeteer opens an empty tab in non-headless mode
Asked Answered
F

7

37

When running puppeteer(last version from npm - 0.13.0) and passing args to

puppeteer.launch({ headless: false })

the chrome is opened with an empty page as a first tab and opens the actual page from the script in the second tab.

const page = await browser.newPage(); Is it an expected behavior? Or a bug?

Freshman answered 10/12, 2017 at 22:55 Comment(0)
J
35

Yes that's expected behavior. It works exactly as opening a chrome browser. If you closed that first tab the browser will close just as using the chrome browser. There needs to be at least one tab open for the browser to remain open. If you use await browser.pages() upon launching the browser, that will return all the pages open currently, which should be 1 about:blank page.

Joline answered 14/12, 2017 at 17:23 Comment(3)
Thank you, I am aware how the chrome browser works. My question is more about why it's leaving the first tab open and empty and opening the actual test page in a second tab, and more of that, how to actually use that first page so there is only one page per browser instance.Freshman
Oh, so let pages = await browser.pages returns an array of pages. If you do that once you launch, the first and only element in the array will be that open tab. So you can assign it to a variable let page = pages[0] and then you can use that page from there.Joline
browser.pages is a function - i.e. let pages = await browser.pages();Clubhaul
M
42

The solution is to use the existing tab/page (dont open a new one):

// launch the browser
var browser = await puppeteer.launch({ headless: false });

// get existing tab/page (first item in the array)
var [page] = await browser.pages();

// load barcode tracking website
await page.goto('https://orcascan.com');
Mahout answered 30/12, 2020 at 22:39 Comment(5)
For me, sometimes the new tab opens up with the page I want to open. And sometimes it just opens the about:blank page. Does this method work fine for you?Such
@UsmanSabuwala yes, this is the method we use within github.com/orca-scan/puppeteer-cucumber-js which we have running in a CI for every commit to orcascan.comMahout
what is the [] need around the variable name ?Ruthenian
It's a destructuring assignment, which is taking the first item of an array.. more info -> developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Mahout
Why use var rather than const?Summerwood
J
35

Yes that's expected behavior. It works exactly as opening a chrome browser. If you closed that first tab the browser will close just as using the chrome browser. There needs to be at least one tab open for the browser to remain open. If you use await browser.pages() upon launching the browser, that will return all the pages open currently, which should be 1 about:blank page.

Joline answered 14/12, 2017 at 17:23 Comment(3)
Thank you, I am aware how the chrome browser works. My question is more about why it's leaving the first tab open and empty and opening the actual test page in a second tab, and more of that, how to actually use that first page so there is only one page per browser instance.Freshman
Oh, so let pages = await browser.pages returns an array of pages. If you do that once you launch, the first and only element in the array will be that open tab. So you can assign it to a variable let page = pages[0] and then you can use that page from there.Joline
browser.pages is a function - i.e. let pages = await browser.pages();Clubhaul
M
5

Try this:

const page = await browser.newPage();
const pages = await browser.pages();
if (pages.length > 1) {
    await pages[0].close();
}
Morty answered 12/12, 2020 at 9:17 Comment(0)
L
2

You can add this to automatically close the first "blank" page whenever you open a new page.

  browser.on('targetcreated', async function f() {

  let pages = await browser.pages();

            if (pages.length > 1) {
                await pages[0].close();

                browser.off('targetcreated', f);
            }

        });
Louettalough answered 19/7, 2020 at 21:58 Comment(1)
You know that the second time you call pages it can return a different list?Pneumectomy
D
2

To me the behavior is unexpected from a user perspective. It may be the design intent, but this would require a response from the developer.

The answer form Bobby Singh is the correct approach given the design of puppeteer Browser class; The usage of puppeteer.launch without args (headless to be specific promises a Browser instance without a blank page, requires awaiting Browser.newPage() in order to Page.goto(url) and subsequent commands; whereas, when declaring headless (whether false or true) the Browser instance promised already has a page loaded. Thus the next natural call for many common to Page.goto(url) opens a second page.

In my experience this causes confusion and was unexpected behavior bordering on a bug. For example, I found that opening a browser with a page in one instance but without a page in another, interfered with timing of further Puppeteer commands.

I once implemented a routine to puppeteer.launch() with no args, then await Browser.newPage(), then page.goto(url), then page.focus(some element). Every thing was working fine, then I wanted to add a debug option, to toggle headless mode. To achieve this I added the headless argument to the original launch(call). Now my session ended up with two pages instead of one. This interfered with the page.focus and subsequent commands on the second page that launched.

The resolution is that if you want to specify headless false or true, you can take the approach of using browser.pages without browser.newPage() but if you don't specify headless, you need to instantiate with Browser.newPage()

Denson answered 17/3, 2021 at 21:11 Comment(0)
V
1

This is default browser behavior, you can easily close empty tab with just single line of code...

Snippet

await (await browser.pages())[0].close();

This will allow you to use 1st tab without opening new!

Snippet

const page = await (await browser.pages())[0];
Venegas answered 5/11, 2022 at 12:33 Comment(0)
G
0

You can pass an URL of the blank page that opens on browser start. Just set it to javascript window.close() function and it will close the blank page immediately even faster then you can notice it.

await puppeteer.launch({
    args: ['javascript:close()'],
});

Also there's an option --no-startup-window which starts browser without first tab opened.

Greenwald answered 6/8, 2023 at 8:41 Comment(1)
Interesting, but since you're going to make a page anyway 99.9% of the time, why not use it rather than closing it?Summerwood

© 2022 - 2024 — McMap. All rights reserved.