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?
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');
5
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.
3
Try this:
const page = await browser.newPage();
const pages = await browser.pages();
if (pages.length > 1) {
await pages[0].close();
}
1
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()
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);
}
});
2
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];
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.
1