Puppeteer opens an empty tab in non-headless mode

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật