30

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?

dre.dev
  • 442
  • 1
  • 4
  • 11

7 Answers7

36

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');
John Doherty
  • 3,669
  • 36
  • 38
  • 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? – Usman Sabuwala Dec 24 '22 at 19:06
  • 1
    @UsmanSabuwala yes, this is the method we use within https://github.com/orca-scan/puppeteer-cucumber-js which we have running in a CI for every commit to orcascan.com – John Doherty Dec 24 '22 at 19:25
  • 1
    what is the `[]` need around the variable name ? – Ahmed Can Unbay Jan 11 '23 at 02:50
  • 1
    It's a destructuring assignment, which is taking the first item of an array.. more info -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – John Doherty Jan 16 '23 at 19:56
33

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.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Bobby Singh
  • 386
  • 3
  • 6
  • 5
    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. – dre.dev Dec 15 '17 at 02:28
  • 15
    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. – Bobby Singh Dec 15 '17 at 16:31
  • 15
    browser.pages is a function - i.e. let pages = await browser.pages(); – Rob Gravelle Dec 14 '18 at 19:35
4

Try this:

const page = await browser.newPage();
const pages = await browser.pages();
if (pages.length > 1) {
    await pages[0].close();
}
Or Assayag
  • 5,662
  • 13
  • 57
  • 93
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);
            }

        });
Hassan Ila
  • 574
  • 1
  • 6
  • 20
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()

scottmains
  • 31
  • 5
0

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

Snippet

await (await browser.pages())[0].close();
Shahnawaz Kadari
  • 1,423
  • 1
  • 12
  • 20
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()'],
});