faker
faker

Reputation: 83

How to change headless from false to true ? [puppeteer]

How to change headless from false to true ? How to hide browser?

const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage();
await page.goto(LOGIN_URL, { "waitUntil": "networkidle2" });
await page.evaluate((a) => {
   $('input[name="username"]').val(a.username)
   $('input[name="password"]').val(a.password)
}, {username, password})
 // I want to hide the browser
 // do something
await browser.close();

Upvotes: 8

Views: 6986

Answers (2)

Vaviloff
Vaviloff

Reputation: 16856

It is not possible to hide the browser in puppeteer at runtime — that's because Chromium can be launched either headless or non-headless only.

But during one script you can first run non-headless browser, close it and then open headless:

let browser = await puppeteer.launch({headless : false});
// 1. Enter requisites, log in to a site
// 2. Save cookies
await browser.close();
browser = await puppeteer.launch({headless : true});
// 3. Load cookies
// 4. Go and do headless stuff

Upvotes: 9

McRist
McRist

Reputation: 1748

Use this:

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

Upvotes: 2

Related Questions