Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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();
like image 829
faker Avatar asked Aug 15 '18 04:08

faker


2 Answers

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
like image 58
Vaviloff Avatar answered Sep 28 '22 06:09

Vaviloff


Use this:

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

like image 25
McRist Avatar answered Sep 28 '22 07:09

McRist